I am using Visual Studio Code, Python, and Tkinter in this program and I want to import and display an image from my computer as a screen(it is in the end). I tried to import the image by copying a statement from a video example I However, when I run the program, it says
tkinter.TclError: bitmap "IMAGE.jpg" not defined
import tkinter
from tkinter import Tk
from PIL import ImageTk, Image
screen = Tk()
screen.iconbitmap("IMAGE.jpg")
As far as I know, .jpg is not supported. Try converting it to .ico format. There are online tools, just upload your .jpg and download .ico.
I found a new solution. see this -
instead of screen.iconbitmap("IMAGE.jpg")
use
root.iconphoto(False, PhotoImage(file='IMAGE.png'))
NOTE
Supports png only
Import PhotoImage from tkinter package
For more detail see my github repo - here
Related
I am trying to create a weather App in Tkinter GUI. It is working fine. I want to add a background image to it but it wont show up. initially i did it without the reference line but it didnt work. On some website they said to keep reference of the image but that too didnt work.
Also my Tkinter window size is 1920x1080 and the image was of same dimension but it still doesnt show up. I tried to reduce image size than window but still not working. Any suggestions accepted. Also there is no error.
bg = PhotoImage('clearsky2.jpg')
bgl = Label(gui,image=bg)
bgl.image = bg #given a reference
bgl.place(x=0, y=0, relwidth=1,relheight=1)
bgl.pack()
Its sad that tkinter.PhotoImage does not support JPEG files, but it does support PNG in the newer version and has proper support for GIF too. To use JPEG you need PIL installed.
In the terminal say:
pip install Pillow
After that import it like:
from PIL import Image,ImageTk
Then now, to open the image with PIL, say:
img_file = Image.open('clearsky2.jpg')
bg = ImageTk.PhotoImage(img_file)
bgl = Label(gui,image=bg)
bgl.place(x=0, y=0, relwidth=1,relheight=1)
This will work with JPEG and PNG files as well and, keep in mind you dont need to keep a reference unless your looping over images or creating image inside of a function.
Hope it cleared your problem, do let me know if any more errors
Cheers
I already finished my project source code. The code is basically about analysis an image. However, I am currently using cv2.imread() to import the image to source code.
I am expecting that user can choose image from Mac finder after I convert the source code to executable.
Basically what I want is:
When user open the executable, Mac finder will show up and user can choose image from it.
I really don't know what to do to achieve this.
You can use tkinter and file dialog to open a finder menu as follows:
from tkinter import filedialog, Tk
root = Tk()
root.withdraw()
path = filedialog.askopenfilename()
I want to make canvas and user will draw some picture in that which I want to save as Image. I am using Python 3.3.2. It doesn't support PIL or Image module. Can someone guide me on this..?
Thanks.
Maybe it's a bit too late to answer this question. But, I'll still go ahead.
from tkinter import *
import pyscreenshot as ImageGrab
r=Tk()
canvas = Canvas(r,height=1000,width=2000,bg="snow")
def getter():
x2=r.winfo_rootx()+canvas.winfo_x()
y2=r.winfo_rooty()+canvas.winfo_y()
x1=x2+canvas.winfo_width()
y1=y2+canvas.winfo_height()
print("save")
ImageGrab.grab().crop((x2,y2,x1,y1)).save("./test.jpg")
b1=Button(r,text="Save",command=lambda:getter())
b1.grid()
Let me know if this doesn't work. Hope it helps..
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.
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.
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')
I am trying to open an image using python; I wrote the following code :
from PIL import Image
im=Image.open("IMG_1930.jpg")
im.show()
But the windows photo viewer opens but it shows the following message instead of the photos:
"windows photo viewer can not open this picture because either the picture is deleted , or it isn't in a location that is accessible."
The show method in PIL is a poor's man way of viewing an image - it has got a hardcoded image viewer application, and writes your image data to a temporary file before calling that as an external application.
What is happening there is that you are either having problems with Windows' uneven access rights policies, and the viewer can't open the file in Python's temporary directory, or there is a problem with Window's problematic path specifications - it might even be a bug in PIL, that renders the temporary paht generated by PIL unusable by the image viewer.
If you are using show in a windowing application, use your tookit's way of viewing images to display it instead - otherwise, if it is a simpler application, build up a Tkitner Window and put the image in it, instead of show.
import sys
import Tkinter
from PIL import Image, ImageTk
window = Tkinter.Tk()
img = Image.open("bla.png")
img.load()
photoimg = ImageTk.PhotoImage(img)
container = Tkinter.Label(window, image=photoimg)
container.pack()
Tkinter.mainloop()
(Linux users: some distributions require the separate install of Tkinter support for PIL/PILLOW. In Fedora, for example, one has to install the python-pillow-tk package )
I also had problems with this. Take a look at this post it fixed my problem: PIL image show() doesn't work on windows 7
Good luck fixing it.