2017-06-18 2 views
1

Si j'ouvre une image comme celle ci-dessous, est-il possible d'agrandir les pixels blancs?Développer les pixels blancs Traitement d'image en python

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)) 

Merci

Répondre

2

Vous pouvez utiliser la fonction dilatent de CV2, et un peu de 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

Cette opération est appelée dilation. Python a des fonctions pour ça.

1

Vous pouvez appliquer une dilatation à l'aide numpy et 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)