Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import PIL.Image, sys
- jflatten = sys.modules[__name__]
- def coords():
- for x in range(w):
- for y in range(h):
- yield (x,y)
- def white_in_image():
- for x,y in coords():
- if image.getpixel((x,y)) == (255,255,255,255):
- return True
- return False
- def gather_regions():
- regions = []
- for x,y in coords():
- px = image.getpixel((x,y))
- if px not in regions and px != (255,255,255,255):
- regions.append(px)
- return regions
- def border_match(pos, color):
- x,y = pos
- if x > 0:
- if image.getpixel((x-1,y)) == color:
- return True
- if x < w-1:
- if image.getpixel((x+1,y)) == color:
- return True
- if y > 0:
- if image.getpixel((x,y-1)) == color:
- return True
- if y < h-1:
- if image.getpixel((x,y+1)) == color:
- return True
- return False
- def grow_region(region):
- border = []
- for x,y in coords():
- px = image.getpixel((x,y))
- if px == (255,255,255,255):
- if border_match((x,y),region):
- border.append((x,y))
- for i in border:
- image.putpixel(i,region)
- def main():
- jflatten.image = PIL.Image.open(sys.argv[1])
- jflatten.w,jflatten.h = image.size
- regions = gather_regions()
- while white_in_image():
- for region in regions:
- print("Growing ", region)
- grow_region(region)
- jflatten.image.save("out.png")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement