Pillow - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Pillow (/thread-23426.html) |
Pillow - juuls04 - Dec-29-2019 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! RE: Pillow - Larz60+ - Dec-29-2019 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__)))
RE: Pillow - juuls04 - Dec-29-2019 (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 outputthanks for your reply! I'm quite new to Python and dont really understand what you mean with look at the 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 RE: Pillow - Larz60+ - Dec-30-2019 The command that I show will display the CWD (current working directory) RE: Pillow - joe_momma - Dec-30-2019 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() |