Search code examples
imagepython-2.7tkinterphotoimage

How does python support the display of images on canvas or label widgets?


The question is not if Python supports the display of images, but rather how it supports the display of images. By choice I am using Python 2.7, and this has Tkinter and Image libraries/modules installed. I don't have the pygame module, which seems to readily support the use of images on the graphical user interface.

To open the file or filename on the system, I am using tkFileDialog and this module works as intended. Then, I can combine the output of the tkFileDialog command with the Image module to create an image object (using the open method). Having done this, I can show my graphic using Image's show method, see below:

import Image
import Tkinter
import tkFileDialog as File
root = Tkinter.Tk()
root.title('Image Test')
root.file = File.askopenfile()
root.image = Image.open(root.file)
root.label = Tkinter.Label(image=root.image)
root.label.grid()

Python uses Image Magick to present a zoomed image, if after line 7, I use

root.image.show()

However, when I attempt to load the image to a Tkinter window using the Label (as illustrated in my code) and PhotoImage widgets (which are described to support image display) python throws a TclError and Runtime error, respectively. The messages are: it is "too early to create image" when using PhotoImage, and "image doesn't exist" when using Label (line 8 in my code). Can anyone provide assistance without suggesting that I add modules to my python (version: 2.7) install?

I am looking to work with bitmaps and jpeg/jpg/jpe images. Preferably, I would like to load the image onto the Canvas object using the create_image method. That is, if and only if I can load the image in to a PhotoImage object (no code included). I will settle for a Label with the image, that will eventually be loaded onto a Canvas.

Useful stackoverflow questions, for reference:

How do I insert a JPEG image into a python Tkinter window?

How to add an image in Tkinter?

Cannot construct tkinter.PhotoImage from PIL Image


Solution

  • To everyone else following, it seems as though one can do just about any image file extension by opening their image using the Image module and programmatically saving that to a gif file type. Then, use the PhotoImage module to load the result to a Canvas widget.

    import Tkinter, Image, tkFileDialog as File
    filename = File.askopenfilename() #choose jpg
    image = Image.open(filename) 
    image.save(fp='somename', format='GIF')
    temporarygif = File.askopenfilename() #choose gift
    photo = Tkinter.PhotoImage(file=temporarygif)
    root = Tkinter.Tk()
    root.title('Simple Image Display')
    root.canvas = Tkinter. Canvas(root, options)
    root.canvas.create_image(0,0,image=photo)
    root.canvas.pack()
    root.mainloop()
    

    Great head turner.

    Problems: the saved gif experiences some loss of color quality. It looks like a 256 color image, and not a full color image.