40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
from PIL import Image
|
|
|
|
img = Image.open("CodeCamembert.png")
|
|
# img = Image.open("exemple_codecamembert.png")
|
|
|
|
dim_camembert = 30
|
|
|
|
width, height = img.size
|
|
x_max = width // dim_camembert
|
|
y_max = height // dim_camembert
|
|
|
|
def fill(image, x, y, base, new):
|
|
global width
|
|
global height
|
|
image.putpixel((x,y), new)
|
|
if x < width-1 and image.getpixel((x+1,y)) == base:
|
|
fill(image, x+1, y, base, new)
|
|
if x > 0 and image.getpixel((x-1,y)) == base:
|
|
fill(image, x-1, y, base, new)
|
|
if y < height-1 and image.getpixel((x,y+1)) == base:
|
|
fill(image, x, y+1, base, new)
|
|
if y > 0 and image.getpixel((x,y-1)) == base:
|
|
fill(image, x, y-1, base, new)
|
|
|
|
res = ""
|
|
for y in range(y_max):
|
|
for x in range(x_max):
|
|
img_tmp = Image.new(img.mode, (dim_camembert, dim_camembert))
|
|
img_tmp.paste(img.crop((x*dim_camembert, y*dim_camembert, x*dim_camembert+dim_camembert, y*dim_camembert+dim_camembert)))
|
|
zones = 0
|
|
for a in range(dim_camembert):
|
|
for b in range(dim_camembert):
|
|
if img_tmp.getpixel((a,b)) == 0:
|
|
fill(img_tmp, a,b,0,255)
|
|
zones += 1
|
|
res += str(zones-1)
|
|
|
|
for i in range(0, len(res), 2):
|
|
c = chr(ord('A') + int(res[i:i+2], 5))
|
|
print(c, end="") |