Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

img_filename = window["image1"].filename AttributeError: 'Image' object has no attribute…

  1. User

    img_filename = window["image1"].filename AttributeError: 'Image' object has no attribute 'filename'

  2. Assistant

    It seems like you're trying to access the 'filename' attribute of an 'Image' object, which does not exist. To resolve this issue, you will need to store the filename separately, before creating the 'Image' object or utilize a different method to get the filename.

    For example, if you are using the tkinter library to open a file dialog, you can save the filename like this:

    import tkinter as tk
    from tkinter import filedialog
    
    def load_image():
        file_path = filedialog.askopenfilename()
        # Now you can create an image object using the file_path
        window['image1'] = tk.PhotoImage(file=file_path)
        # Store the filename separately
        window['image1'].filename = file_path
    
    root = tk.Tk()
    button = tk.Button(root, text="Load Image", command=load_image)
    button.pack()
    root.mainloop()
    

    By doing this, you will be able to access the filename like this:

    img_filename = window["image1"].filename