I have been trying to resize images using PIL then display them using Tkinter, but the program has been crashing and I've isolated the problem to the second line below:
image = Image.open("0.gif")
photo = ImageTk.PhotoImage(image)
And this is my imports:
from Tkinter import *
from PIL import Image, ImageTk
I've read around that Tk must be initialized and I do this in the program before it reaches those lines in the program. So I don't know what it is.
I am running OSX and python 2.7 interpreter on eclipse (using PyDev).
UPDATE:
Error message on eclipse says:
STACK: Stack after current is in use
I've seen that error before using tkinter. I think it had something to do with an older version of tkinter. I updated my python version and tkinter version and it went away. Does this error happen when you run your code on a different OS/Computer/Platform/Version of Python? What version of tkinter are you using? Some google searching revealed these two pages which describe the same bug while using tkinter...
http://osdir.com/ml/python.leo.general/2008-03/msg00060.html
http://fornax.phys.unm.edu/lwa/trac/ticket/3
I can't see all your code, but I'm betting that there is not necessarily anything wrong with your code. The following code worked for me...
from Tkinter import *
from PIL import Image, ImageTk
# resize image with PIL
im = Image.open('path to gif')
resized_im = im.resize((400,400,),Image.ANTIALIAS)
# display image in tkinter window
window = Tk()
tk_im = ImageTk.PhotoImage(resized_im)
window.geometry('%dx%d' % (resized_im.size[0],resized_im.size[1]))
label_image = Label(window, image=tk_im)
label_image.place(x=0,y=0,width=resized_im.size[0],height=resized_im.size[1])
window.mainloop()
Using....
ubuntu 10.04 64 bit
python 2.6.5
python-imaging-tk 1.1.7
python-tk 2.6.5 (which uses version 8.5.0 of tkinter)
python imaging library (PIL) 1.1.7
eclipse 3.7.1
pydev 2.5.0.2012050419
Good luck!
I've been using both Tk, PIL and resizing images for a current project and the following code works fine for me.
#Imports
from Tkinter import *
from PIL import Image, ImageTk
#Create Tk instance
root = Tk()
#Open image and resize
image = Image.open("path/to/image/file").resize((400,400), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
After that, I find it easiest to display the images as labels in tkinter like so.
image_label = Label(root, width = 400, height = 400, image = photo bd = 0)
(I like the bd = 0 as otherwise I get a thin white border around the image.)
Hope this has helped you. Good luck!
Ed
So this is an ancient question, but in case someone stumbles upon this (like I just did), the error message is from Tcl (tclExecute.c). I have no idea what's triggering it, but one thing worth trying is to create a Tk instance before calling PhotoImage:
root = Tk()
image = Image.open("0.gif")
photo = ImageTk.PhotoImage(image)
Related
Goal: make "tomato.png"(the image on the left) the background of the window on the right.
The window wouldn't even come up when I'd run the code & I'd get this message:
_tkinter.TclError: couldn't recognize data in image file "tomato.png"
In previous work with tkinter I used tkk. to make words show up on a button I was using, I tried it here to no avail(This method was me hip-firing. I've only been studying Python since Dec, 31- there's a lot I do not know.)
I googled my issue and have a cursory knowledge of Pil/Pillow...so I installed Pillow through Pycharm.
There was some progress made. After installing Pillow through Pycharm the window actually came up, the error message did not, but...the image did not produce.
Also, this is the path to the python I am using: /Users/my_name/.pyenv/versions/3.10.0/bin/python
I would appreciate all the help you can give and keep in mind...I'm just two weeks into python with a background in finance- I am not pally with programming parlance...at all. Even when researching this issue there were suggestions to install pip through the command line and to be frank, I had no idea what was going on.
Also, this is my first post so don't crucify me if I didn't follow SOP in this post. Some of you guys are ruthless when answering questions.
I appreciate your time.
https://i.stack.imgur.com/BCehz.png
the link to the photo of my code should be here^^
But I manually entered it here:
from tkinter import
from tkinter import ttk
from PIL import Image
from PIL import ImageTk
# ---------------------------- CONSTANTS ------------------------------- #
PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
FONT_NAME = "Courier"
WORK_MIN = 25
SHORT_BREAK_MIN = 5
LONG_BREAK_MIN = 20
# ---------------------------- TIMER RESET ------------------------------- #
# ---------------------------- TIMER MECHANISM ------------------------------- #
# ---------------------------- COUNTDOWN MECHANISM ------------------------------- #
# ---------------------------- UI SETUP ------------------------------- #
window = Tk()
window.title("Pomodoro")
canvas = Canvas(width=200, height=224)
tomato_pic = Image.PhotoImage(file="tomato.png")
canvas.create_image(100, 112, image=tomato_pic)
canvas.pack()
window.mainloop()
I found an alternative here:
https://stackoverflow.com/a/46624625/8146119
import Tkinter as tk
from PIL import ImageTk, Image
window = tk.Tk()
window.title("tkinter stuff")
window.geometry("300x300")
window.configure(background='grey')
path = "hs.gif"
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(window, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
window.mainloop()
I'll have to find out what makes this code work and what makes my code fail.
## UPDATE ##
The issue was that I wasn't using the appropriate tkinter, I was using 8.5 when I should have been using 8.6. I was also using python 3.10 and since tkinter, based on my cursory searches online, doesn't work on 3.9. I am using 3.8. The issue I was having is no longer.
Hopefully someone will benefit from this. I appreciate everyone's time.
I found this post because I had the same problem. I am a two month beginner in python. I have changed the photo format to jpeg and now it works nice, but it losts transparent background...
I used it in similar code like the previous one:
from tkinter import *
from PIL import ImageTk
window = Tk()
window.title("Pomodoro")
canvas = Canvas(width=200, height=224)
tomato_pic = Image.PhotoImage(file="tomato.jpeg")# notice .jpeg instead of .png format
canvas.create_image(100, 112, image=tomato_pic)
canvas.pack()
window.mainloop()
I hope it helps.
I've tried with different .png files and sometimes works and others not...
I'm really happy to have found this site, many thanks everyone!
First, you may have better luck with newer versions of Python. Various stackoverflow posters have noted issues with tkinter supporting images. See post here
According to this stackoverflow answer the tkinter.Label and Label.place are handy tricks for placing background images in tkinter windows. Below is how this may fit into your code. It works for me using Python 3.8 and Windows 10.
from tkinter import *
from PIL import Image
from PIL import ImageTk
# ---------------------------- CONSTANTS ------------------------------- #
PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
FONT_NAME = "Courier"
WORK_MIN = 25
SHORT_BREAK_MIN = 5
LONG_BREAK_MIN = 20
# ---------------------------- TIMER RESET ------------------------------- #
# ---------------------------- TIMER MECHANISM ------------------------------- #
# ---------------------------- COUNTDOWN MECHANISM ------------------------------- #
# ---------------------------- UI SETUP ------------------------------- #
window = Tk()
window.title("Pomodoro")
canvas = Canvas(width=200, height=224)
tomato_pic = PhotoImage(file="tomato.png")
# note here use of Label
background_label = Label(window, image=tomato_pic)
# label.place is tk trick that is good for backgrounds
background_label.place(x=0, y=0, relwidth=1, relheight=1)
canvas.pack()
window.mainloop()
Then you may need to play with the relwidth, relheight parameters and possibly shrink the size of your original image depending to get it to fit properly.
from tkinter import *
from tkinter import ttk
win = Tk()
colorful=PhotoImage(file='image/1.png')
_tkinter.TclError: couldn't recognize data in image file "image/1.png" file path is correct, from beginning it was running without an error but when I add some more code it happened, why?
Try this:
from PIL import Image, ImageTk
img = Image.open("file_name")
colorful = ImageTk.PhotoImage(img)
Tkinter Photoimage only supports .gif images.
If you want to display .png images, Use PIL (Python Imaging Library)
Fore more info, see https://pythonbasics.org/tkinter-image/ .
I've spent some time looking online and so far haven't found anything that answers this without using PIL, which i can't get to work is there another way to do this simply?
tkinter has a PhotoImage class which can be used to display GIF images. If you want other formats (other than the rather outdated PGM/PPM formats) you'll need to use something like PIL or Pillow.
import Tkinter as tk
root = tk.Tk()
image = tk.PhotoImage(file="myfile.gif")
label = tk.Label(image=image)
label.pack()
root.mainloop()
It's important to note that if you move this code into a function, you may have problems. The above code works because it runs in the global scope. With a function the image object may get garbage-collected when the function exits, which will cause the label to appear blank.
There's an easy workaround (save a persistent reference to the image object), but the point of the above code is to show the simplest code possible.
For more information see this web page: Why do my Tkinter images not appear?
You can use PIL library which available for python 3.
First, you need to install PIL using this command (using pip):
pip install pillow
then:
`from tkinter import *`
from PIL import Image, ImageTk
`class Window(Frame):`
`def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack(fill=BOTH, expand=1)
load = Image.open("parrot.jpg")
render = ImageTk.PhotoImage(load)
img = Label(self, image=render)
img.image = render
img.place(x=0, y=0)`
Luke, this is too late , however may help others. Your image has to be in the same sub directory with .py script. You can also type './imageFileName.png'
The following code makes Python "quit unexpectedly" when trying to create the PhotoImage instance (it prints 1 and quits). I'm on OS X 10.9.5, using Python 2.7.10, ActiveTcl 8.6.4 from ActiveState, running the script from IDLE using Run / Run Module. Any clue? I'm totally new to Python and all the GUI modules
import numpy as np
import collections
import math
import Tkinter
from PIL import Image, ImageTk
# A root window for displaying objects
root = Tkinter.Tk()
# Convert the Image object into a TkPhoto object
im = Image.open('samples.png')
print 1
imgtk = ImageTk.PhotoImage(image=im)
print 2
# Put it in the display window
Tkinter.Label(root, image=imgtk).pack()
root.mainloop()
ImageTk.PhotoImage seems to be broken, at least on some systems including Python 2.7 on OS X Sierra. You can use Tkinter.PhotoImage instead, but it only takes a raw file as an argument which is pitiful.
did you try something link this:
import Tkinter
from PIL import Image, ImageTk
root = Tkinter.Tk()
imgtk = ImageTk.PhotoImage(file='test.jpg')
Tkinter.Label(root, image=imgtk).pack()
root.mainloop()
I want to display a gif image using Pillow
Here is my simple code:
from tkinter import *
from PIL import Image, ImageTk
import tkinter as Tk
image = Image.open("Puissance4.gif")
image.show()
But nothing happens...
All help will be appreciated
Thanks!
PIL provides a show method which attempts to detect your OS and choose an
appropriate viewer. On Unix it tries calling the imagemagick command display
or xv. On Macs it uses open, on Windows it uses... something else.
If it can't find an appropriate viewer, ImageShow._viewers will be an empty list.
On Raspbian, you'll need to install an image viewer such as display, xv or fim. (Note a search on the web will show that there are many image viewers available.) Then
you can tell PIL to use it by specifying the command parameter:
image.show(command='fim')
To display an image in Tkinter, you could use something like:
from PIL import Image, ImageTk
import tkinter as tk
root = tk.Tk()
img = Image.open("image.gif")
tkimage = ImageTk.PhotoImage(img)
tk.Label(root, image=tkimage).pack()
root.mainloop()