Python Forum

Full Version: Pillow
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
from tkinter import *
from PIL import Image,ImageTk

root = Tk()

img = ImageTk.PhotoImage(Image.open('red-circle.png'))
label = Label(image=my_img)
label.pack

root.mainloop()
When I run this, I get this error: FileNotFoundError: [Errno 2] No such file or directory: 'red-circle.png'.

I want to upload an image into my program and I saw that Pillow (PIL) was a good option. I've been trying for half an hour now but i just dont know how i can upload/import an image into my program. Does somebody know what is the best way to do this?
Thanks in advance!
You are probably not in the same directory as the image file, even though you mayy think you are.
import os and look at cwd output
import os

...
print(f"{os.getcwd()}")
If this is the case, it can be corrected by adding the following line before line 6
os.chdir(os.path.abspath(os.path.dirname(__file__)))
(Dec-29-2019, 09:30 PM)Larz60+ Wrote: [ -> ]You are probably not in the same directory as the image file, even though you mayy think you are. import os and look at cwd output
 import os ... print(f"{os.getcwd()}") 
If this is the case, it can be corrected by adding the following line before line 6 os.chdir(os.path.abspath(os.path.dirname(__file__)))
thanks for your reply! I'm quite new to Python and dont really understand what you mean with look at the cwd output?
The command that I show will display the CWD (current working directory)
your code is incomplete and wrong in several spots-
from tkinter import *
from PIL import Image,ImageTk
 
root = Tk()
 
img = ImageTk.PhotoImage(Image.open('red-circle.png'))
# you're reference is img
label = Label(image=my_img) #now you call it my_img try label=Label(root,image=img)
#if you use pack it's a function so label.pack() or label.pack(side='top')
label.pack
 
root.mainloop()
if your photo or photo object isn't in the same folder(your cwd) you'll need to put in the path to it. example C://Mydocuments/image/round-red.png that's a string.
take a look at guizero documentation
the same output as above is:
from guizero import App, Picture
app = App()
picture = Picture(app, image="red-circle.png")
app.display()