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()
Related
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 3 months ago.
I created a button with an image in Tkinter, but the image isn't showing, i can just see a blank space in the button. Maybe my OS is the problem. I have Linux Mint 21 Xfce Edition.
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
from tkinter import dialog
from tkinter import *
from PIL import ImageTk, Image
from modules import fonts, buttons
import shutil
import os
virusscan_win = Tk()
virusscan_win.title("Virus scan")
virusscan_win.geometry('400x200+60+60')
virusscan_win.configure(background="white")
Message(text="The virus scanner gives them the ability to scan files, folders or their system for viruses and remove threats automatically.", width=250, font=fonts.sansserif_small, background = "white").place(x=10, y=10)
safe_image=PhotoImage(file='./media/button.png')
Button(master=virusscan_win, image=safe_image, text="Scan file").place(x=10, y=90)
Can anyone explain, what was wrong with my code? I already checked, is the file in the correct folder. the debugger also shows nothing.
Python is notorious for aggressively garbage collecting image references, which leads to them being destroyed before the UI gets drawn. Something like this should work for getting an image onto a Button
from PIL import Image, ImageTk
from pathlib import Path
img_path = Path('C:/<path>/<to>/media/button.png') # use the full path to your img file
with Image.open(img_path) as img: # open the image file
img_tk = ImageTk.PhotoImage(img) # get the image as a Tk object
button = Button(virusscan_win, image=img_tk) # add image to button
Note that if your image is the wrong size for your button, you can resize it like so
img_tk = ImageTk.PhotoImage(img.resize(32, 32)) # or whatever size you need
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 am new to coding so problem might be very simple but I just dont get it.
So I am trying to upload a gif image to tkinter label. Here is my code:
from PIL import *
import PIL.Image
import tkinter as tk
from tkinter import *
root = Tk()
img = PIL.Image.open ("10.gif")
tkimage = PhotoImage(img)
label = Label(root, image=tkimage)
label.pack()
root.mainloop()
And here is the error that I am getting:
__str__ returned non-string (type GifImageFile)
I dont seem to find answer from google for this particular problem(I have tried)
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()
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)