0

I made a GUI for my model predicting my images after it was trained by CIFAR10 dataset. I want when I add the image to GUI, it appears on my Canvas area. However, the image was not shown. But when I click my 'classify' button, it still appears correct result. So it is definitely the display image problem. I also have searched and tried many way on the Internet but it did not help very well. Can someone tell me how to fix it? All comments are very appreciated. Thank you in advance!

My GUI when predicting. As you can see, it still has correct output, but the image doesn't show (it should show at the position of the white square - canvas)

My code:

# I just keep the overall structure of the code 
# as some parts are not related to this problem
import ...

def get_image():
    # Get path
    path = askopenfilename(filetype=(("jpg file", "*.jpg"), ("png file", '*.png'), ("All files", "*.*"),))

    # Load image
    img = Image.open(path)
    img = img.resize((350, 350))
    img = ImageTk.PhotoImage(img)

    return path, img


class App(tk.Tk):
    def __init__(self): 

    # Code of 'browse' button
    def add_image(self):
        # Erase past record
        self.delete_image()

        # Load image
        path, img = get_image()

        # Display path and image
        self.text.insert(END, path)
        self.canvas.create_image(0, 0, anchor=NW, image=img)

    def classify_image(self):

    def delete_image(self):

app = App()
mainloop()

JonnyJack
  • 109
  • 8
  • As CoolCloud pointed out, use `path, self.img = get_image()` instead of `path, img = get_image()` – TheLizzard Jun 15 '21 at 17:10
  • Yes, it fix my problem. When I was searching, I did not see this post. Thank you so much! – JonnyJack Jun 15 '21 at 17:14
  • For someone who maybe will see this post later, I fix my code by adding `global img` above line `img = Image.open(path)` as someone has pointed it out in the another post (which appear at the top of this post) – JonnyJack Jun 15 '21 at 17:22

0 Answers0