Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pillow
#1
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!
Reply
#2
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__)))
Reply
#3
(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?
Reply
#4
The command that I show will display the CWD (current working directory)
Reply
#5
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()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020