Display image in Gui

I need to implement a function in Gui so that you click the buttuon mirror button, it calls the flip function and displays img in Gui.
Suppose the image inserted in mirror / flip is B & W img.
I checked flip and it works fine, but not when Im trying to merge it into Gui .
What am I missing?

 def flip(im): '''Flips a picutre horizontally, and returns a new image that is a mirror view of the original''' org=Image.open(im) new=Image.new("L",org.size) for x in range(org.size[0]): for y in range(org.size[1]): pixel=org.getpixel((x,y)) new.putpixel((org.size[0]-x-1,y),pixel) return new def mirror(): '''Flips the image like a mirror does, left to right''' global img out = Image.new('L',img.size, 'white') out=flip(img) img = out display() def display(): delete() global img global photo photo = ImageTk.PhotoImage(img) canvas.create_image(250, 250, image=photo) canvas.pack() ### GUI packing ### g = Gui() g.title('PhotoPy 0.2') global img ### canvas canvas=g.ca(500, 500, bg='white') ### menu g.row(weights=[1,0,0,0]) filename = g.en(text='python.bmp', width=16) g.bu(text='...', command=browse) g.bu(text='Save', command=save) g.bu(text='Load', command=load) g.bu(text='Mirror', command=mirror) g.endrow() ### start g.mainloop() 

I get this msg error:

 Exception in Tkinter callback Traceback (most recent call last): File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__ return self.func(*args) ... line 87, in mirror out=flip(img) ... line 24, in flip org=Image.open(im) ... line 1956, in open prefix = fp.read(16) ... in __getattr__ raise AttributeError(name) AttributeError: read 
0
source share
1 answer

Assuming Image is pil.Image , the open function expects a file name or file object as an argument. In your code, img is probably None , since you haven't set it anywhere.

In another note, you should not use a global variable for an image or use global variables at all if you are not sure what you really need. Refactoring your code into a class that has an image as an attribute. Also, declaring photo as global in display simply not important, since you only use it in display .

+1
source

Source: https://habr.com/ru/post/1492475/


All Articles