Tkinter and PIL not loading jpegs - python

I'm trying to allow my Tkinter program to load and display .jpgs files from the standard .gif
import Image,ImageTk
root = Tk()
PILFile = Image.open("Image.jpg")
Image = PhotoImage(file=PILFile)
ImageLabel = Label(root,image=Image)
ImageLabel.image = Image
ImageLabel.pack()
root.mainloop()
The error message I get follows:
Traceback (most recent call last):
File "/home/paragon/Programs/Python/Web/MP3Tagger.py", line 25, in <module>
albumart = PhotoImage(file=PILfile)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 3271, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 3227, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "<JpegImagePlugin.JpegImageFile image mode=RGB size=1500x1500 at 0x98AEFCC>": no such file or directory
[Finished in 0.4s with exit code 1]
I am absouletly certain that the file exists in the correct format,what could I be doing wrong?

According to The Tkinter PhotoImage Class:
The PhotoImage class can read GIF and PGM/PPM images from files:
....
If you need to work with other file formats, the Python Imaging Library (PIL) contains classes that lets you load images in over 30 formats, and convert them to Tkinter-compatible image objects:
from PIL import Image, ImageTk
image = Image.open("lenna.jpg")
photo = ImageTk.PhotoImage(image)
-> Replace Tkinter.PhotoImage with ImageTk.PhotoImage:
root = Tk()
PILFile = Image.open("Image.jpg")
Image = ImageTk.PhotoImage(PILFile) # <---
ImageLabel = Label(root, image=Image)
ImageLabel.image = Image
ImageLabel.pack()
root.mainloop()

Your error occurs because of the argument file:
Image = PhotoImage(file=PILFile)
This would specify a file path. In addition, you want ImageTk.PhotoImage. Instead, you want:
Image = ImageTk.PhotoImage(PILFile)

Related

Custom Tkinter Images

I'm trying to create a back button. I have an image called back-button.png in the folder img.
This is my code:
from tkinter import *
import customtkinter as ctk
root = Tk()
ctk.CTkLabel(root,
text = 'This is a label',
text_font =('Verdana', 17)).pack(side = LEFT, pady = 11)
img = PhotoImage(file="./img/back-button.png")
ctk.CTkButton(root, image = img).pack(side = LEFT)
root.mainloop()
When I run this code I get this error:
Traceback (most recent call last):
File "c:\Users\User\Desktop\youtube-audio-downloader\tempCodeRunnerFile.py", line 11, in <module>
ctk.CTkButton(root, image = img).pack(side = LEFT)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\customtkinter\customtkinter_button.py", line 102, in __init__
self.draw()
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\customtkinter\customtkinter_button.py", line 147, in draw
self.canvas.configure(bg=CTkColorManager.single_color(self.bg_color, self.appearance_mode))
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1646, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1636, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown color name "."
So, why is this happening? And how can I display an image on a button?
The problem is that the CtkButton widget doesn't not accept parameters the same way as standard widgets. The first parameter to a CtkButton is the background color, but you're passing the root window and the root window isn't a valid color.
You need to explicitly assign the root window to the master argument.
ctk.CTkButton(master=root, image = img).pack(side = LEFT)
# ^^^^^^^
You are getting error because you are using built-in image define method which is in Tkinter not in Customtkinter. Don't confuse these two. If you want to use Customtkinter only use that because when you are making bigger projects than this, it will be a whole mess if you use these two together.
import customtkinter
from PIL import Image, ImageTk
window = customtkinter.CTk()
button_image = customtkinter.CTkImage(Image.open("assets/yourimage.png"), size=(26, 26))
image_button = customtkinter.CTkButton(master=window, text="Text will be gone if you don't use compound attribute",image=button_image)
image_button.pack()
window.mainloop()

tkinker error: couldn´t recognize data in image file

I´m new to python and have problems with tkinker and the Images.
My error is:
Traceback (most recent call last):
File "D:/python/First Project/Weather app.py", line 12, in <module>
background_image = tk.PhotoImage(file='landscape.jpg')
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3498, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "landscape.jpg"
and the associated code is:
import tkinter as tk
root = tk.Tk()
background_image = tk.PhotoImage(file='D:\python\First Project\landscape.jpg')
background_label = tk.Label(root, image=background_image)
background_label.place(relwidth=1, relheight=1)
root.mainloop()
The ending of the file isn´t renamed and originally .jpg.
I also tried to edit it with
background_image = ImageTk.PhotoImage(Image.open('your.png'))
The PhotoImage method does not appear to support JPG files. I received the same error (couldn't recognise data in "image.jpg") when using a JPG file. When using a PNG file I did not receive any such errors.
Note that you cannot just change the file extension of a JPG file to turn it into a PNG file, because the data in PNG files is different from a JPG file. You will need to convert your JPG file to a PNG (or any other image file type supported by the PhotoImage method.)

Images not showing up in tkinter GUI window

Basically I'm having trouble opening images (.jpg) in a tkinter GUI window.
I followed this tutorial: https://www.youtube.com/watch?v=lt78_05hHSk
But I just get a blank window.
from tkinter import *
root = Tk()
photo = PhotoImage(file=r"C:\Users\xxxx\OneDrive\Desktop\123k.jpg")
# photo = PhotoImage(file="123k.jpg")
label = Label(root, image=photo)
label.pack()
root.mainloop()
The file path is correct and .jpg files are compatible with tkinter. What could be going on?
I get this error
Traceback (most recent call last):
File "C:\Users\xxx\ytrewq.py", line 5,
in <module> photo = PhotoImage(file=r"C:\Users\xxxx\OneDrive\Desktop\123k.jpg")
File "C:\Users\xxxx\AppData\Local\Programs\Thonny\lib\tkinter_init_.py", line 3545,
in init Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\xxxx\AppData\Local\Programs\Thonny\lib\tkinter_init_.py", line 3501,
in init self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file
"C:\Users\xxxx\OneDrive\Desktop\123k.jpg"
This error occurs because only PhotoImage function can't recognize the .jpg formate image. For this, you could use png formate or you could use
Image.open(path_of_the_file) and then
ImageTk.PhotoImage(img_object)
for this Image and ImageTk you have to import this from PIL module
from PIL import Image, ImageTk
The hole solution code:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
# photo = PhotoImage(file=r"C:\Users\xxxx\OneDrive\Desktop\123k.jpg")
# photo = PhotoImage(file="123k.jpg")
img = Image.open(file=r"C:\Users\xxxx\OneDrive\Desktop\123k.jpg")
photo = ImageTk.PhotoImage(img)
label = Label(root, image=photo)
label.pack()
root.mainloop()

Tkinter Label image setting won´t work

I've been learning how to use Tkinter from scratch and while I try to set a simple Label widget in a frame:
from Tkinter import *
from ttk import *
root = Tk()
root.title("Practice")
mainW = LabelFrame(root, text = "Main info")
mainW.grid()
image = Label(mainW, image = "C:\Users\Oscar Ramirez\Pictures\image.png")
image.grid(column = 0, row = 0)
codeEntry = Entry(mainW, text = "User Code")
codeEntry.grid(column = 1, row = 0)
root.mainloop()
I'm getting the following error:
Traceback (most recent call last):
File "Tutorial.py", line 10, in <module>
image = Label(mainW, image = "C:\Users\Oscar Ramirez\Pictures\image.png")
File "C:\Python27\lib\lib-tk\ttk.py", line 757, in __init__
Widget.__init__(self, master, "ttk::label", kw)
File "C:\Python27\lib\lib-tk\ttk.py", line 555, in __init__
Tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2096, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image specification must contain an odd number of elements
I've checked the image format, the route, etc. And now I don't really know what can be causing me trouble.
image
The image to display in the widget. The value should be a
PhotoImage, BitmapImage, or a compatible object. If specified, this
takes precedence over the text and bitmap options. (image/Image)
Right now you are just passing a string for image option of label. You need something like,
photo = PhotoImage(file="image.gif")
label = Label(..., image=photo)
label.photo = photo #reference keeping is important when working with images
Right now, since you are using PNG image, you need to install and use Python Imaging Library (PIL) though. For more info, you can read Photo Image section from effbot.

Importing image into Tkinter - Python 3.5

I'm majorly struggling to import an image into Tkinter for Python 3.5, this is for my A2 project and have hit a brick wall with every method of importing a .JPG file to my window. What I have below is my GUI layer for another window, based off another thread I found but didn't work at all. Any assistance appreciated.
import tkinter
from tkinter import *
root=Tk()
root.geometry('1000x700')
root.resizable(False, False)
root.title("Skyrim: After Alduin")
photo = PhotoImage(file="Skyrim_Map")
map=Label(root, image=photo)
map.photo=photo
map.pack()
root.mainloop()
Here's the error I receive:
Traceback (most recent call last):
File "E:\Programming\Text_Adventure_Project\GUI_Interface-S_AA.py", line 14, in <module>
photo = PhotoImage(file="Skyrim_Map")
File "C:\Users\Harrison\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 3393, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Harrison\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 3349, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "Skyrim_Map": no such file or directory
First check the path if with the correct path still persist the error "no such file or directory" please edit and put the newest version of your code.
If the error is "couldn't recognize data in image file" you can correct using PIL
[...]
from PIL import ImageTk
[...]
photo = ImageTk.PhotoImage(file='Skyrim_Map.jpg')
[...]
Try this:
from PIL import Image , ImageTk
img = Image.open("your_img")
image = ImageTk.PhotoImage(img)
An other point of view :
class Application:
...
self.photo=PhotoImage(file='picture.gif')
self.item=self.can.create_image(200, 100, image=self.photo)
...
Hope to help ! ;)

Categories