Work for small images...

This commit is contained in:
Francois JUMELLE
2021-05-05 00:23:06 +02:00
parent 7ca7d0d5a1
commit 6eefe326fc

View File

@@ -1,99 +1,102 @@
from PIL import Image
img_original = Image.open("laketour2.png")
img_original = Image.open("exlaketour.png")
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_original.size
width +=2
height += 2
img = Image.new(img_original.mode, (width, height))
img.paste(img_original.crop(), (1,1))
width, height = img.size
img = img.convert("RGB")
def contour(x, y, pixel_to_fill, pixels_to_match):
def fill(x, y, pixels_to_match, pixel_to_fill, remplir_countour=False):
global img
global width
global height
res = 1
countour = ???
if y > 0 and img.getpixel((x,y-1)) in pixels_to_match:
status, pixels, color1 = contour(x, y-1, pixel_to_fill, pixels_to_match)
if not status:
return False, 0, 0
res += pixels
if x < width-1 and img.getpixel((x+1,y)) in pixels_to_match:
status, pixels, color2 = contour(x+1, y, pixel_to_fill, pixels_to_match)
if not status:
return False, 0, 0
if color2 != color1:
return False, 0, 0
res += pixels
if x > 0 and img.getpixel((x-1,y)) in pixels_to_match:
status, pixels, color3 = contour(x-1, y, pixel_to_fill, pixels_to_match)
if not status:
return False, 0, 0
if color3 != color1 or color3 != color2:
return False, 0, 0
res += pixels
if y < height-1 and img.getpixel((x,y+1)) in pixels_to_match:
status, pixels, color4 = contour(x, y+1, pixel_to_fill, pixels_to_match)
if not status:
return False, 0, 0
if color4 != color1 or color4 != color2 or color4 != color3:
return False, 0, 0
res += pixels
return True, res, color1
def fill(x, y, pixel_to_fill, pixels_to_match):
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
res += 1
contour = list()
if p in pixels_to_match:
if y > 0 and img.getpixel((x,y-1)) in pixels_to_match:
pixels = fill(x, y-1, pixel_to_fill, pixels_to_match)
res += pixels
if x < width-1 and img.getpixel((x+1,y)) in pixels_to_match:
pixels = fill(x+1, y, pixel_to_fill, pixels_to_match)
res += pixels
if x > 0 and img.getpixel((x-1,y)) in pixels_to_match:
pixels = fill(x-1, y, pixel_to_fill, pixels_to_match)
res += pixels
if y < height-1 and img.getpixel((x,y+1)) in pixels_to_match:
pixels = fill(x, y+1, pixel_to_fill, 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
return res
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)) == noir:
pixels = fill(x,y, bleu, [noir,])
print(pixels)
status, pixels_countour, color = contour(x, y, bleu, [vert,])
print(status, pixels_countour, color)
img.show()
if status:
fleches += (pixels+pixels_countour) * color
else:
fill(x,y, rouge, [bleu,])
img.show()
if img.getpixel((x, y)) != rouge:
fleches += img.getpixel((x, y))[0]
print(fleches, "flèches")
img.save("laketour2_tmp.png")
img.show()
print(fleches)