zeeks
zeeks

Reputation: 794

Expand white pixels Image processing in python

If I open an image as the one below, is it possible to expand the white pixels?

enter image description here

enter image description here

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

Answers (3)

Snow
Snow

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

fepegar
fepegar

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

yar
yar

Reputation: 1916

This operation is called dilation. Python has functions for that.

Upvotes: 2

Related Questions