2017-06-18 86 views
1

如果我打開下圖所示的圖像,是否可以展開白色像素?展開白色像素python中的圖像處理

enter image description here

enter image description here

僞代碼:

image = Image.open("test.png") 
image = image.convert("RGBA") 
my_data = image.getdata() 

new_data = [] 
for i in my_data: 
    if i[0] == 255 and i[1] == 255 and i[2] == 255: 
     #append white to new_data + to the pixels around 
    else: 
     new_data.append((255, 255, 255, 0)) 

謝謝

回答

2

可以使用擴張功能從CV2,有點numpy的的:

import cv2 
import numpy as np 

img = cv2.imread('input.png',0) 
kernel = np.ones((5,5), np.uint8) 
dilation = cv2.dilate(img,kernel,iterations = 5) 

cv2.imwrite('result.png', dilation) 
1

這種操作稱爲dilation。 Python有這個功能。

1

可以使用numpyscipy應用擴張:

import numpy as np 
from scipy.ndimage.morphology import binary_dilation 

# Create and initialize image 
image = np.zeros((21, 41), dtype=bool) 
image[8:12, 18:22] = True 

# Define structuring element and applying dilation 
strel = np.ones((3, 3)) 
dilated = binary_dilation(image, structure=strel)