from PIL import Image img = Image.open("laketour2.png") img = Image.open("laketour2_fju.png") # img = Image.open("exlaketour.png") noir = (0, 0, 0) rouge = (255, 0, 0) vert = (0, 255, 0) bleu = (0, 0, 255) width, height = img.size img = img.convert("RGB") def fill(x, y, pixels_to_match, pixel_to_fill, remplir_countour=False): global img global width global height res = 0 p = img.getpixel((x,y)) if remplir_countour or p in pixels_to_match: img.putpixel((x,y), pixel_to_fill) res += 1 contour = list() if p in pixels_to_match: if y > 0 and img.getpixel((x,y-1)) in pixels_to_match: pixels, new_contour = fill(x, y-1, pixels_to_match, pixel_to_fill, remplir_countour) res += pixels contour += new_contour if y > 0 and img.getpixel((x,y-1)) not in pixels_to_match and img.getpixel((x,y-1)) != pixel_to_fill: contour.append((x,y-1)) if remplir_countour: img.putpixel((x,y-1), pixel_to_fill) res += 1 if x < width-1 and img.getpixel((x+1,y)) in pixels_to_match: pixels, new_contour = fill(x+1, y, pixels_to_match, pixel_to_fill, remplir_countour) res += pixels contour += new_contour if x < width-1 and img.getpixel((x+1,y)) not in pixels_to_match and img.getpixel((x+1,y)) != pixel_to_fill: contour.append((x+1,y)) if remplir_countour: img.putpixel((x+1,y), pixel_to_fill) res += 1 if x > 0 and img.getpixel((x-1,y)) in pixels_to_match: pixels, new_contour = fill(x-1, y, pixels_to_match, pixel_to_fill, remplir_countour) res += pixels contour += new_contour if x > 0 and img.getpixel((x-1,y)) not in pixels_to_match and img.getpixel((x-1,y)) != pixel_to_fill: contour.append((x-1,y)) if remplir_countour: img.putpixel((x-1,y), pixel_to_fill) res += 1 if y < height-1 and img.getpixel((x,y+1)) in pixels_to_match: pixels, new_contour = fill(x, y+1, pixels_to_match, pixel_to_fill, remplir_countour) res += pixels contour += new_contour if y < height-1 and img.getpixel((x,y+1)) not in pixels_to_match and img.getpixel((x,y+1)) != pixel_to_fill: contour.append((x,y+1)) if remplir_countour: img.putpixel((x,y+1), pixel_to_fill) res += 1 if x == 0 or y == 0 or x == width-1 or y == height-1: contour.append(None) return res, contour #On commence par remplir toutes les zones en vert, une à une #en regardant si elles ont un seul type de contour ou plusieurs #si plusieurs, ce ne sont pas des lacs, on les passe en rouge #sinon on les passe de la couleur du contour for x in range(width): for y in range(height): if img.getpixel((x, y)) == noir: pixels, contour = fill(x, y, [noir,], vert) # on passe en vert les zones noires et on liste les points du contour contour_colors = list({img.getpixel((p[0], p[1])) for p in contour if p != None}) # print(pixels, contour, contour_colors) if None in contour: fill(x, y, [vert,], rouge) elif len(contour_colors) != 1: fill(x, y, [vert,], rouge) else: fill(x, y, [vert,], contour_colors[0]) # img.show() fleches = 0 for x in range(width): for y in range(height): if img.getpixel((x, y)) != rouge: fleches += img.getpixel((x, y))[0] print(fleches, "flèches") img.save("laketour2_tmp.png") img.show()