-1

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

Phume
  • 55
  • 11
  • 1
    It might be helpful to post the code you're using to make it easier for others to see what exactly you're doing – CertainPerformance Aug 05 '18 at 02:11
  • @CertainPerformance I needed this guide on formatting; https://meta.stackexchange.com/questions/88541/how-to-write-code-in-a-question-or-answer If you can, please excuse me. – Phume Aug 06 '18 at 23:44
  • The following is not an answer, rather a comment to emphasize that I need help with **tkinter** and **Image** rather than alternatives like _pygame_; GTK/GTK+ (pygtk/gtk) also has a perfect function for image display; gtk.Image().set_from_file('image name') – Phume Aug 07 '18 at 00:20
  • Your initial sentence asks a question that is way too broad and not well related to your actual question, "How do I use Image Magic with tkinter." – Terry Jan Reedy Aug 07 '18 at 00:34
  • @TerryJanReedy no that is not my question at all. My question is the described methods of using Tkinter to show images do not work. I made it broad intentionally so as not to be presumptuous in how Tkinter should work. – Phume Aug 08 '18 at 12:39
  • You never use the the documented and working methods for image creation in the code shown. See http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/images.html. In tk 8.6, add .png to the supported image formats. – Terry Jan Reedy Aug 09 '18 at 03:16
  • @TerryJanReedy I am using a manual from *New Mexico Tech* called *Tkinter 8.5 reference* (as a supplement to _python cli help_). Alright, I can scale back my code to a version that will only display *gif* file extensions, using the *PhotoImage* widget (probably where I started), and the *create_image* function on the canvas widget. However, I want to be able to display *ANY* image file extension with the application that I am making. Especially, as the most common file format is *jpg* or *jpeg* file format. – Phume Aug 14 '18 at 19:51
  • Slightly different approaches if the PhotoImage and Gif file type are the end; •https://stackoverflow.com/questions/24004363/convert-images-to-gif •https://stackoverflow.com/questions/33010401/need-help-converting-jpg-link-to-gif-format – Phume Aug 15 '18 at 02:26
  • You should be able to create a Photoimage from PIL. I don't know about Image Magick. – Terry Jan Reedy Aug 16 '18 at 06:18
  • That is the problem, I cannot. Dare I say it looks like it is impossible to use anything because Canvas will only accept PhotoImage and PhotoImage will only accept GIFs not jpeg nor objects such as that which one gets with Image module. To reiterate, the Image module seems to have Image Magick embedded in it. Evident when you use the show function to display the opened image! @All I am just about ready to close this question as impossible. – Phume Aug 16 '18 at 16:23

1 Answers1

0

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.

Phume
  • 55
  • 11