python - unable to add image to the GUI(tkinter) on windows - python

I am using python( my version is 2.7 ). I want to add an image to GUI (Tkinter) and then convert into executable format using pyinstaller.
I did followed as on SO, and also as said on ActiveState
When i mention the image's path on the code, it works only if i run it directly. If i convert it to exe it doesnt open.
Changing the code as mentioned from other solutions, like by converting it into encoded string, it runs fine on linux. But on windows it throws error
code:
from Tkinter import *
from PIL import ImageTk, Image
logo = '''
----- encoded string -----
'''
root = Tk()
logoimage = Tkinter.PhotoImage(master=root, data=logo)
Label(root, image=logoimage).pack()
root.mainloop()
Change 1:
The above code works on linux. On windows i get error on the line logoimage = Tkinter.PhotoImage(master=root, data=logo) as
NameError: name 'Tkinter' is not defined
Change 2:
So i tries changing the line as logoimage = ImageTk.PhotoImage(master=root, data=logo). The error i get is
File "C:\Python27\lib\site-packages\PIL\ImageTk.py", line 88, in __init__
image = Image.open(BytesIO(kw["data"]))
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2330, in open
% (filename if filename else fp))
IOError: cannot identify image file <_io.BytesIO object at 0x00000000024BB150>
Exception AttributeError: "'PhotoImage' object has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <PIL.ImageTk.PhotoImage object at 0x00000000024D49E8>> ignored
Change 3:
But, if i change the line as iconImage= ImageTk.PhotoImage(Image.open('path_to_image.png')). It works only if i run directly. If i convert it to executable, then console opens for 2-3 seconds and displaying error something like Unable to locate the image file

Doing the decoding and converting explicitly may be more robust than what you're currently doing. This code works on Python 2.6.6 on Linux.
import io, base64
from Tkinter import *
from PIL import ImageTk, Image
#A simple 64x64 PNG fading from orange in the top left corner
# to red in the bottom right, encoded in base64
logo_b64 = '''
iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIA
AAAlC+aJAAAA/0lEQVR4nO3Zyw7CMAxEUdP//+W2rCqBoJA2noclS1kn9yjLeex7xKY76+
wNS+l6KSCjXgdIqhcB8uoVgNR6OiC7ngsA1BMBmHoWAFZPASDr8QBwPRiAr0cCKPUwAKse
AyDWAwDc+mwAvT4VoKjPA4jqkwC6+gyAtD7WSYC6fu4HDOonAB71dwE29bcATvXXAWb1Fw
F+9VcAlvXDANf6MYBx/QDAu/4fwL7+J6BC/TmgSP0JoE79N0Cp+g9Atfp3QMH6F0DN+gNQ
tj62WErXB2PgQNZLAb3U6wC91OsAvdTrAL3U6wC91OsAvdTrAL3U6wC91OsAvdTrAL3Uz7
z+BNmX4gqbppsaAAAAAElFTkSuQmCC
'''
#Decode the PNG data & "wrap" it into a file-like object
fh = io.BytesIO(base64.b64decode(logo_b64))
#Create a PIL image from the PNG data
img = Image.open(fh, mode='r')
#We must open the window before calling ImageTk.PhotoImage
root = Tk()
photo = ImageTk.PhotoImage(image=img)
Label(root, image=photo).pack()
Label(root, text='An embedded\nbase64-encoded PNG').pack()
root.mainloop()
For reference, here's what that embedded PNG looks like.

from Tkinter import *
#...
logoimage = Tkinter.PhotoImage(master=root, data=logo)
If you dump the Tkinter module straight into the global scope using import *, then you shouldn't prefix class and function names with the module name. Either remove the prefix, or remove the import *.
import Tkinter
#...
logoimage = Tkinter.PhotoImage(master=root, data=logo)
Or
from Tkinter import *
#...
logoimage = PhotoImage(master=root, data=logo)
I suspect you're not getting the error in Linux because your version of Python imports common modules automatically. Effectively, there's an invisible import Tkinter at the top of all your scripts.

Related

How to display display an IMAGE on a CANVAS using PYTHON's "tkinter" module, Specifically for linux?

I've used the following code:
# A Python program to display images in canvas
from tkinter import *
# create a root window
root = Tk()
# create a canvas as a child to the root window
c = Canvas(root,bg='black',height=700,width=1200)
# copy images into files
file1 = PhotoImage(file='PYTHON/Graphical User Interface[GUI]/Containers/cat.png')
file2 = PhotoImage(file='PYTHON/Graphical User Interface[GUI]/Containers/puppy.png')
# Display the Image in the canvas in NE direction
# when mouse is placed on cat image, we can see puppy image
id = c.create_image(500,200,ANCHOR=NE,image=file1,activeimage=file2)
# display some text below the image
id = c.create_text(500,500, text= "Displaying Image Demo in tkinter",\
font = ('Helvetica',30,'bold'), fill='blue')
# add canvas to the root
c.pack()
# wait for any events
root.mainloop()
When executed, the above code shows the following error in VScode:
Traceback (most recent call last):
File "/DATA/CodeTrainings/PYTHON/Graphical User Interface[GUI]/Containers/canvasImage(linux).py", line 12, in <module>
file2 = PhotoImage(file='PYTHON/Graphical User Interface[GUI]/Containers/puppy.png')
File "/home/lancelot/anaconda3/envs/pyTrain/lib/python3.8/tkinter/__init__.py", line 4061, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/home/lancelot/anaconda3/envs/pyTrain/lib/python3.8/tkinter/__init__.py", line 4006, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "PYTHON/Graphical User Interface[GUI]/Containers/puppy.png": no such file or directory
It says that any such file isn't present in the directory but it is indeed present:
And, everything works fine in windows, but when it comes to image files in LINUX an error always pops up!
Thanks if anyone could answer my question, but a detailed explanation or pointers on how to handle image files and what are the constraints while working with image files on LINUX is well appreciated.
Thank You!
Haha!
I have everything rightly setup, but the only thing where I went wrong is that, while downloading the files, I just renamed the file formats without actually converting them to the required image file format.
This was the problem that caused the error.
I used the online converters to convert the image and everything worked as it has to.
I've learned that renaming doesn't change the internal formatting in the files, therefore it is necessary to convert them using a converter into appropriate formats.
Please correct me if I'm wrong.
Thank You!

Image not displayed on python

i am a beginner of python programming.i will place the images on the frame. but image not not diaplayed error shown below.
Traceback (most recent call last):
File "C:/Users/kobinath/PycharmProjects/pythonProject4/jj.py", line 5, in <module>
img = PhotoImage(file="pic.jpg")
File "C:\Users\kobinath\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 4061, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\kobinath\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 4006, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "pic.jpg"
what i tried so far i attached below.
from tkinter import *
root = Tk()
canvas = Canvas(root, width = 600, height = 600)
canvas.pack()
img = PhotoImage(file="pic.jpg")
canvas.create_image(20,20, anchor=NW, image=img)
root.mainloop()
I actually think its not possible to use jpg with PhotoImage directly, instead you might want to use PIL and heres how,
pip install PIL
After this, just say
from tkinter import *
from PIL import Image,ImageTk
root = Tk()
canvas = Canvas(root, width = 600, height = 600)
canvas.pack()
img_file = Image.open("sad songs.jpg")
img_file = img_file.resize((150,150)) #(width,height)
img = ImageTk.PhotoImage(img_file)
canvas.create_image(20,20, anchor=NW, image=img)
root.mainloop()
Here is a site to convert jpg to gif
Do let me know, if any errors or doubts
Cheers
It may be a result of a new bug thats been going on with tkinter images where we have to keep a reference of the image to make sure it will work I hope this will fix it.
canvas.img = img
Also as #Cool Cloud has also pointed out it can also be the problem where it does not work with jpg files sometimes but for me sometimes it does too, so if its not working with you, you can just convert it into a png or gif(I would prefer png based on what has worked with me when I ran into these problems).
Also instead of the conversion you can try the PIL library it can be installed using
pip install PIL
And then you can use a class called PhotoImage(yup it has the same name as the tkinter class) to do the image loading and stuff, which is under the module ImageTk which can be used to deal with tkinter images.
Also you can pass the image object into this class as a parameter, you can create an Image object like so-:
img_obj = Image.open('annoying_image.jpg') # uses the Image module from PIL use import PIL.Image
Like so
img = PIL.ImageTk.PhotoImage(img_obj) # uses the Image module from PIL use import PIL.ImageTk
Also If you want to know more about PIL, its just basically a Image Processing Library of python often called as pillow also.
IMPORT STATEMENTS(JUST IN CASE ITS NOT CLEAR)
from PIL import Image, ImageTk

How to save an image as a variable?

Now, I have a python game that has sprites, and it obtains the images from files in its directory. I want to make it such that I do not even need the files. Somehow, to pre-store the image in a variable so that i can call it from within the program, without the help of the additional .gif files
The actual way i am using the image is
image = PIL.Image.open('image.gif')
So it would be helpful if you could be precise about how to replace this code
Continuing #eatmeimadanish's thoughts, you can do it manually:
import base64
with open('image.gif', 'rb') as imagefile:
base64string = base64.b64encode(imagefile.read()).decode('ascii')
print(base64string) # print base64string to console
# Will look something like:
# iVBORw0KGgoAAAANS ... qQMAAAAASUVORK5CYII=
# or save it to a file
with open('testfile.txt', 'w') as outputfile:
outputfile.write(base64string)
# Then make a simple test program:
from tkinter import *
root = Tk()
# Paste the ascii representation into the program
photo = 'iVBORw0KGgoAAAANS ... qQMAAAAASUVORK5CYII='
img = PhotoImage(data=photo)
label = Label(root, image=img).pack()
This is with tkinter PhotoImage though, but I'm sure you can figure out how to make it work with PIL.
Here is how you can open it using PIL. You need a bytes representation of it, then PIL can open a file like object of it.
import base64
from PIL import Image
import io
with open("picture.png", "rb") as file:
img = base64.b64encode(file.read())
img = Image.open(io.BytesIO(img))
img.show()

python GTK3 Image from PIL jpg format

I'm trying to open image (jpg) from the buffer (get as blob from Oracle database) as Gtk.Image() and add it to a Gtk window, but I get error "Expected Gtk.Widget, but got PIL.JpegImagePlugin.JpegImageFile". I can show the image with show(), I can window.add jpeg file from path on the disc, and then show the window, but when I try to add jpg from buffer I get the error. Here is what I produced:
my_source=Here_I_get_BLOB_img_from_database()
newimage=Gtk.Image()
newimage=my_source.read()
image=io.BytesIO(newimage)
dt=Image.open(image)
newwindow = Gtk.Window(modal=True)
In this point actually I have jpg in buffer and I can do:
dt.show() # to display the image in system imageviewer
Or save dt as jpg, or add to newwindow a result of image.set_from_file("path with jpg extension") but don't know how to do this:
newwindow.add(dt)
Or anything with similar effect. How to do this in simplest way?
What worked for me was -
Gtk.Image to load img from buffer has Pixbuf object, which for example can be loaded from stream. So:
from gi.repository import (...) GdkPixbuf, GLib, Gio
(...)
my_source=Here_I_get_BLOB_img_from_database()
newimage=my_source.read()
glib=GLib.Bytes.new(newimage)
stream = Gio.MemoryInputStream.new_from_bytes(glib)
pixbuf = GdkPixbuf.Pixbuf.new_from_stream(stream, None)
image=Gtk.Image().new_from_pixbuf(pixbuf)
my_window = Gtk.Window(modal=True, title="Image")
my_window.add(image)
image.show()
my_window.show()

No file or Directory exist, but it does

I am trying to use tkinter module to open a window to show a picture and was previously having trouble with: _tkinter.TclError: couldn't recognize data in image
I have since used the following this question to restructured my code to the following:
How do I insert a JPEG image into a python Tkinter window?
I have made a few changes to the code as I am using python 3.
When I have tried to execute the code: I get an error saying no file or directory exist. However i have used the following code to check:
python open() method IOError: [Errno 2] No such file or directory:
Below are the result of checking the files:
>>> os.listdir()
['comments.py', 'Conan.txt', 'conditions.py', 'dateandtime.py', 'desktop.ini', 'dictionaries.py', 'exceptions.py', 'forging_functions.py', 'formatting.py', 'graphics.py', 'Hello.py', 'leapdays.py', 'logging.py', 'loop.py', 'modules.py', 'months.py', 'numbers.py.py', 'Opening_files.py', 'Picture.jpg', 'print_hello.py.py', 'tkintercode1.py', 'tkintercode2.py', 'user_input.py', 'Writing_file.py', '__pycache__']
>>> os.getcwd()
'C:\\Users\\Draco\\OneDrive\\Documents\\Programming'
>>> os.chdir(r'C:\\Users\\Draco\\OneDrive\\Documents\\Programming')
>>> open('Picture.jpg')
<_io.TextIOWrapper name='Picture.jpg' mode='r' encoding='cp1252'>
Below is the code I am working with:
import tkinter as tk
from tkinter import *
window = tk.Tk()
window.title("Random Image")
window.geometry("300x300")
window.configure(background='grey')
path = "C:\Draco\OneDrive\Documents\Programming\Picture.jpg"
img = tkinter.PhotoImage(Image.open(path))
panel = tk.Label(window, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
window.mainloop()
Many thanks for any help
"C:\Draco\OneDrive\Documents\Programming\Picture.jpg" try path ="C:\\Draco\\OneDrive\\Documents\\Programming\\Picture.jpg" - double slash
EDIT
You have diff in paths. Users\Draco but in code just Draco

Categories