Home > front end >  Picture won't open in File Dialog, Tkinter, Pillow
Picture won't open in File Dialog, Tkinter, Pillow

Time:02-03

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

CodePudding user response:

askopenfilenames return a tuple.

Try this instead

my_image = ImageTk.PhotoImage(Image.open(root.filename[0]))

CodePudding user response:

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.

  •  Tags:  
  • Related