I'm trying to get tkinter to open an image selected by the user from a the file browser prompt and display it on the canvas. I plan to edit this image later so I'm using PIL to edit and support other file types. However, when I try to display the image, nothing shows up and no errors are thrown. I don't really know why this is so. I think it might be because Image is a module name shared in both tkinter and PIL but I don't know for sure.
Code:
from tkinter import *
from tkinter import filedialog
from PIL import ImageTk, Image
root = Tk()
canvas = Canvas(root, width=300, height=300)
canvas.pack(expand=YES, fill=BOTH)
img = Image.open(
filedialog.askopenfilename(title="Select file", filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*"))))
canvas.create_image(0, 0, image=ImageTk.PhotoImage(img), anchor=NW)
canvas.update()
root.mainloop()
I found that if you split the line
canvas.create_image(0, 0, image=ImageTk.PhotoImage(img), anchor=NW)
into 2 separate lines
img = ImageTk.PhotoImage(img)
canvas.create_image(0, 0, image=img, anchor=NW)
would work, I am not sure why but it is a quick fix.
Related
While trying to create an app including an image within a canvas by Tkinter, I got some problems with resizing the image. The first one is that "from PIL import Image, ImageTk" does not work in PyCharm IDE, and shows "ModuleNotFoundError: No module named 'PIL'". The second one is not understandable for me since I am a novice in coding. It happened when I run the python file that I mentioned in cmd.
Could you help me to understand the problem and what I can do with it?
Code:
from tkinter import*
import tkinter
from PIL import Image, ImageTk
image = Image.open("BG.png")
image = image.resize((500,500), Image.ANTIALIAS)
#self.pw.pic = ImageTk.PhotoImage(image)
myCanvas = Canvas(root, bg = "white", height=600, width = 600)
myCanvas.place(x=550,y=100)#pack()
myCanvas.create_image(0,0, image=image,anchor="nw")
myCanvas.place(x=550, y=100)
app=Window(root)
root.mainloop()
Error shown in cmd:
File "tk.py", line 50, in <module>
myCanvas.create_image(0,0, image=image,anchor="nw")
File "C:\Users\hahik_zvw4rds\anaconda3\lib\tkinter\__init__.py", line 2785, in create_image
return self._create('image', args, kw)
File "C:\Users\hahik_zvw4rds\anaconda3\lib\tkinter\__init__.py", line 2771, in _create
return self.tk.getint(self.tk.call(
_tkinter.TclError: image "<PIL.Image.Image image mode=RGBA size=500x500 at 0x1A1BB564550>" doesn't exist
Your commented out code is correct. You need to convert the Image to a PhotoImage for tkinter to be able to use it.
from tkinter import*
import tkinter
from PIL import Image, ImageTk
image = Image.open("BG.png")
image = image.resize((500,500), Image.ANTIALIAS)
pic = ImageTk.PhotoImage(image)
myCanvas = Canvas(root, bg = "white", height=600, width = 600)
myCanvas.place(x=550,y=100)#pack()
myCanvas.create_image(0,0, image=pic, anchor="nw")
myCanvas.place(x=550, y=100)
myCanvas.image=pic
app=Window(root)
root.mainloop()
I'm new here, so I'm hoping to ask correctly.
I want to program a GUI on a raspberry pi in fullscreen mode with python. I managed to display a background image. Now I'm trying to get a simple text on it, but I'm not managing to do that. I searched a lot but didn't find any solution to my problem. Pls help!
Here my code:
from tkinter import *
from PIL import ImageTk
window = Tk()
C = canvas(window, height = 500, width=800)
C.pack()
image = ImageTk.PhotoImage(file = "home/pi/Downloads/background3.jpg")
background_label = Label(window, image = image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
Label(window, text="Hello", fg="black").pack()
window.attributes('-fullscreen', True)
windows.mainloop()
Thank you in advance!
You need to use the create_image and create_text methods of the canvas to overlay text on an image. Just add your image, the text you want and the fullscreen command to the below example to get what you want.
from tkinter import *
from PIL import ImageTk
window = Tk()
c = Canvas(window,height=450,width=750)
image = ImageTk.PhotoImage(file="YourImage.png")
c.create_image(0,0,image=image,anchor=NW)
c.create_text(200,200,text="Hello World",fill="yellow",font="Times 32 bold")
c.pack()
window.mainloop()
I have been trying to show an image for a game but it doesn't show can i please have some help?
I've tried to download PIL but it doesn't work with python 3.
from tkinter import *
canvas = Canvas(width=500, height=500)
canvas.pack(expand=YES, fill=BOTH)
Logo = PhotoImage('photo.gif')
canvas.create_image(50, 10, image=Logo)
label = Label(image=Logo)
label.image = Logo
label.pack()
mainloop()
If you dont tell PhotoImage() what the first argunent is, it will assume its the widget's name. To get it to load a file you have to explicitly tell it that it is a file:
Logo = PhotoImage(file='photo.gif')
Python 3.6.4 with Pillow 5.2.0
You can use: python -m pip install Pillow
Here is an example of how to use PIL with tkinter
from PIL import Image, ImageTk
import tkinter as tk
root = tk.Tk()
img = Image.open("test.png")
tk_image = ImageTk.PhotoImage(img)
label =tk.Label(root, image=tk_image)
label.pack()
root.mainloop()
I made a GUI with few buttons and I want to change the gray background to an image.
my code looks like this:
from tkinter import *
from urlread import givenumbers # my function
""" Setting The Main GUI """
GUI = Tk()
GUI.title('Check GUI')
GUI.iconbitmap('test.ico')
GUI.geometry("400x400")
background_image=PhotoImage('pic.jpg')
background_label = Label(GUI, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
""" Reading Images For Buttons """
A_Im = PhotoImage(file='A.gif')
""" Creating Buttons """
# A Button
A_Button = Button(GUI, image=A_Im, command=givenumbers)
A_Button.grid(column=0, row=1)
GUI.mainloop()
The code runs without error but the background is still gray without any effect.
The problem is in the line background_image=PhotoImage('pic.jpg'). The PhotoImage class only supports GIF-files, which means that it cannot read the file you're specifying. You should try something like this:
#Python 2.7
import Tkinter as tk
from PIL import Image, ImageTk
window = tk.Tk()
image = Image.open('image.jpg')
photo_image = ImageTk.PhotoImage(image)
label = tk.Label(window, image = photo_image)
label.pack()
# Python 3
import tkinter as tk
from PIL import Image, ImageTk
window = tk.Tk()
image = Image.open('image.jpg')
photo_image = ImageTk.PhotoImage(image)
label = tk.Label(window, image = photo_image)
label.pack()
The Image class from the PIL module supports a variety of formats, among which jpeg and png. You can install the PIL module by running pip install pillow in a command prompt or terminal.
If you want to put the widgets on top of the Label, you could indeed using grid to get them on top of each other, but using a Canvas would probably be easier. You can find more about the Canvas widget here.
This can be done without pil also:
from tkinter import *
import tkinter as ttk
""" Setting The Main GUI """
GUI = Tk()
F1=Frame(GUI)
F1=Frame(GUI,width=400,height=450)
F1.place(height=7000, width=4000, x=100, y=100)
F1.config()
F1.grid(columnspan=10,rowspan=10)
F1.grid_rowconfigure(0,weight=1)
F1.grid_columnconfigure(0,weight=1)
photo=PhotoImage(file="C:\\Users\\HOME\\Desktop\\Eshita\\12th\\computer
\\python\\GUI\\math3.gif")
label = Label(GUI,image = photo)
label.image = photo # keep a reference!
label.grid(row=0,column=0,columnspan=20,rowspan=20)
b=ttk.Button(GUI,text="Start")
b.grid(row=8,column=8)
GUI.mainloop()
I am trying to load images dynamically through browse button to Tkinter window but I am getting the empty window. This is the code of callback function of browse button
supformats = [
('Windows Bitmap','*.bmp'),
('Portable Network Graphics','*.png'),
('JPEG ','*.jpg'),
('CompuServer GIF','*.gif'),
]
filename = askopenfilename(filetypes=supformats)
FILENAME = filename
im=Image.open(FILENAME)
w=im.size[0]
h=im.size[1]
root = Tkinter.Tk()
#canvas = Tkinter.Canvas(root, width=w, height=h)
#canvas.grid(row=0,column=0)
#tk_img = ImageTk.PhotoImage(file = FILENAME)
#canvas.create_image(image=tk_img)
im.show()
root.mainloop()
Thanks in advance for everyone who will help
From effbot's explanation http://effbot.org/tkinterbook/photoimage.htm Note that the file is accessed once only, not twice. This assumes you are using the Python Imaging Library in Tkinter as you did not state what image toolkit is being used.
from PIL import Image, ImageTk
image = Image.open("lenna.jpg")
photo = ImageTk.PhotoImage(image)
You can use a PhotoImage instance everywhere Tkinter accepts an image object.
An example:
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()