1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import numpy as np
from PIL import Image
img = Image.open("carre.png")
array = np.array(img)
x,y = (94,91) # Coordonnées du coin haut-gauche du carré
h = 68 # Hauteur du carré
color = (237, 28, 36) # Point rouge à trouver
crop = array[y:y+h, x:x+h] # Tableau 2D du carré vert uniquement (pas besoin de chercher dans le contour noir)
for Y, line in enumerate(crop):
for X, pixel in enumerate(line):
if (pixel == color).all():
print(f"Point rouge trouvé aux coordonnées : {(x+X,y+Y)}")
crop[Y][X] = np.array((45,45,250)) # Point rouge devient bleu
#Sauvegarder la nouvelle image
im = Image.fromarray(array)
im.save("carre2.png") |
Partager