Issue
I am currently working on a tkinter (GUI) project that recieves the user's average and returns it to them. I wish to display images in my parent window using the PIL library. Yesterday this library was working fine and locating my image in the directory, but today it seems to not be able to find the directory, can someone please help me out and guide me. I don't know why the PIL library is acting up today, usually it's working fine. (I've tried reinstalling the files but not help!).
Here is my code
import tkinter
from PIL import Image, ImageTk
root = tkinter.Tk();
def showImg():
load = Image.open('Desktop\example.jpg')
render = ImageTk.PhotoImage(load)
img = tkinter.Label(root, image = render)
img.image = render
img.pack()
button = tkinter.Button(root, text='Click me to see an image.', command=showImg).pack();
root.title('Imaging test');
root.geometry('450x450');
root.mainloop();
Here is my error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:\Users\Pamal\Desktop\Documents\Python Folder\Python [Learn]\imaging_example.py", line 7, in showImg
load = Image.open('Desktop\example.jpg')
File "C:\Python34\lib\site-packages\PIL\Image.py", line 2219, in open
fp = builtins.open(fp, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'Desktop\\example.jpg
The text FileNotFoundError: [Errno 2] No such file or directory: 'Desktop\example.jpg' makes no sense to me. Just yesterday when I was using the PIL library, I was able to display images fine without receiving this error message, but today all I'm getting is this error message. I have even tried specifying the file path up to the C:\ but it won't accept it. Please someone help me out I need to get images in my tkinter (GUI) parent window but this library won't allow it. (P.S I have tried reinstalling PIL and it hasn't changed a bit, if you know any [easy to use] imaging libraries, please inform me of them).
You should know:
- I am running on Python 3.4.2
- Windows 8.1 machine
- I am not an Python expert, so please don't show me some complex code.
Below is an image of my most recent error that i'm receiving. I've supplied a specific location to the image, and yet im getting this annoying error. ALSO SOMEONE EXPLAIN WHY THE DOUBLE [ // ] SLASHES ARE THERE EVEN THOUGH I ENTERED ONLY 1. DOES THAT EFFECT MY FILES LOCATION? I'VE HIGHLIGHTED THEM IN YELLOW, SORRY FOR THE CAPS, I'M JUST TRYNA GET YOUR ATTENTION AND ALSO I'M NEW TO STACK OVERFLOW SO WOOPS :P
Does it have something to do with this line:
Solution
From your traceback your script seems to be located in - C:\Users\Pamal\Desktop\Documents\Python Folder\Python [Learn]\imaging_example.py
But you are trying to access something inside (Desktop folder) using relative path - Desktop\example.jpg
.
This would not work, unless your script is inside - C:\Users\Pamal
.
Better would be to give absolute path, such as - C:\Users\Pamal\Desktop\example.jpg
.
Code -
load = Image.open(r'C:\Users\Pamal\Desktop\example.jpg')
Answered By - Anand S Kumar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.