0

I have written this.

Its short and supposed to open a file dialog window, with buttons "open a file, turn, save, exit." I want to open a jpeg, turn it 180° and then save it and quit.

The program starts, but doesnt open the picture in the file dialog window after I select it in the browser.

from tkinter import *
from PIL import Image, ImageTk
from tkinter import ttk
from tkinter import filedialog
from tkinter.filedialog import askopenfilenames


root = Tk() ##pro Tkinter

root.title('Image Browser')
root.geometry('100x100')


def open():
    global my_image
    root.filename = filedialog.askopenfilenames(initialdir='/gui/pillow', filetypes=[('Images','*.jpg *.jpeg *.png')])
    #my_label = Label(root, text = root.filename).pack()
    my_image = ImageTk.PhotoImage(Image.open(root.filename))
    my_image_label = Label(image=my_image).pack()

def turn():
    return
    my_image = my_image.transpose(Image.FLIP_RIGHT_LEFT)

def save():
    return
    my_image.save('somepic.jpg')


button = Button (root, text = 'Select a File', command = open)
button.pack()
button4 = Button (root, text = 'Turn', command = turn)
button4.pack()
button2 = Button (root, text = 'Save', command = save)
button2.pack()
button3 = Button(root, text = 'Exit', command = jadro.quit)
button3.pack()


root.mainloop()

After I select and try to open the file, it says this

AttributeError: 'tuple' object has no attribute 'seek'

I feel like I've tried everything, but cant seem to solve it. Thanks for any help

Banshek
  • 1
  • 1
  • 1
    Please show the full exception trace. That will show exactly where this error is occurring – DarkKnight Feb 02 '22 at 12:19
  • Its here: Traceback (most recent call last): File "C:\Users\RJ\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__ return self.func(*args) File "c:\Users\RJ\Desktop\pillow\main_buttons.py", line 20, in open my_image = ImageTk.PhotoImage(Image.open(root.filename)) File "c:\Users\RJ\Desktop\pillow\venv\lib\site-packages\PIL\Image.py", line 2981, in open fp = io.BytesIO(fp.read()) AttributeError: 'tuple' object has no attribute 'read' – Banshek Feb 02 '22 at 12:41
  • Welcome to Stack Overflow. Regarding the error message: 1. That doesn't match what you wrote in the title. 2. You should include it *in the post itself*, *formatted as code*. Use the `Edit` link under your post to add the information. – Karl Knechtel Feb 02 '22 at 13:08
  • Also: please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and try to diagnose and trace the problem yourself before posting. For example: when `root.filename = filedialog.askopenfilenames(initialdir='/gui/pillow', filetypes=[('Images','*.jpg *.jpeg *.png')])` happens, what do you expect the value of `root.filename` to look like? *Does* it? *Did you check*? If it doesn't meet your expectation, the next step is to *read the documentation* for `filedialog.askopenfilenames`. – Karl Knechtel Feb 02 '22 at 13:10
  • Of course I've tried for several hours, otherwise I wouldnt be here? The thing is that I dont see the problem, thats why I came here to seek help. – Banshek Feb 02 '22 at 13:37

2 Answers2

1

askopenfilenames return a tuple.

Try this instead

my_image = ImageTk.PhotoImage(Image.open(root.filename[0]))
scotty3785
  • 6,763
  • 1
  • 25
  • 35
1

From Python DOC

tkinter.filedialog.askopenfilename(**options)
tkinter.filedialog.askopenfilenames(**options)

The above two functions create an Open dialog and return the selected filename(s) that correspond to existing file(s).

Therefore askopenfilenames() returns a tuple of filenames, instead Image.open(fp) requires a file pointer as argument, not a tuple.

From Pillow DOC:

PIL.Image.open(fp, mode='r', formats=None)

fp – A filename (string), pathlib.Path object or a file object. The file object must implement file.read, file.seek, and file.tell methods, and be opened in binary mode.

Marco Valle
  • 176
  • 6