开发者

Coloring an edge detected image by user input co-ordinates..??What's wrong with this code?

开发者 https://www.devze.com 2023-01-24 23:06 出处:网络
I need to color the white part surrounded by black edges! from PIL import Image import sys image=Image.open(\"G:/ghgh.bmp\")

I need to color the white part surrounded by black edges!

from PIL import Image
import sys
image=Image.open("G:/ghgh.bmp")
data=image.load()

image_width,image_height=image.size   

sys.setrecursionlimit(10115)

def f(x,y):
    if(x<image_width and y<image_height and x>0 and y>0):
        if (data[x,y]==255):
            image.putpixel((x,y),150) 
            f(x+1,y)
            f(x-1,y)
     开发者_高级运维       f(x,y+1)
            f(x,y-1)
            f(x+1,y+1)
            f(x-1,y-1)
            f(x+1,y-1)
            f(x-1,y+1)
f(100,100)
image.show()

255 is to detect white color, 150 is used to re-color greyish, and (100,100) is the starting pixel.

It's giving "Max recursion depth" at n=10114 and python crashes on n=10115 (the setrecursionlimit(n)).


What's up with the while loop? It never changes the coordinates, so it seems it would loop forever, doing recursive calls. From a quick reading it sounds like it should be a plain if.

Also: drop the parens with while and if; they're not needed in Python.


"unwind" above is right. Just replace your "while" with an "if" and it should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消