Reputation: 1
I need to display images within a tk window, but can only use these functions to do it;
from urllib.request import urlopen
from re import findall, MULTILINE, DOTALL
from webbrowser import open as webopen
from os import getcwd
from os.path import normpath
from tkinter import *
from sqlite3 import *
from datetime import datetime
I was thinking that there might be some way I can do this just through tkinter's standard functions, but have yet to find any possible way.
This or I could try and find a way to convert the image to a GIF. Note that no images can be stored on the system.
The images I need to display are either JPGs or PNGs, such as this one.
Any help is appreciated.
Upvotes: 0
Views: 289
Reputation: 16169
The Tcl/Tk module Img
allow you to load JPEG and PNG (among other formats) with PhotoImage. This module is included in some packaged distributions of Tcl/Tk. So it is worth checking whether it is installed on the computer you are using. To do so, try to load it with:
widget.tk.eval('package require Img')
widget
can be any Tkinter widget.
If it returns a version number, then it is installed and loaded so you can use it and open JPEG images simply doing
PhotoImage(file='/my/image.jpg')
Upvotes: 0
Reputation: 4730
As is stated here:
The PhotoImage class can read GIF and PGM/PPM images from files:
photo = PhotoImage(file="image.gif") photo = PhotoImage(file="lenna.pgm")
The PhotoImage can also read base64-encoded GIF files from strings. You can use this to embed images in Python source code (use functions in the base64 module to convert binary data to base64-encoded strings):
And as was pointed out in the comments below and here you can also use PNG images with PhotoImage
.
Images in tkinter have to be passed in as PhotoImage
objects to be drawn and in order to get a PhotoImage
object, the image has to be one of the above formats.
So you will need to find a way to get the images in one of those formats using the modules you have available.
Upvotes: 1