Reputation: 794
If I open an image as the one below, is it possible to expand the white pixels?
Pseudocode:
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))
Thank you
Upvotes: 1
Views: 1558
Reputation: 1138
You can use the dilate function from cv2, and a bit of 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)
Upvotes: 1
Reputation: 693
You can apply a dilation using numpy
and scipy
:
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)
Upvotes: 3