Python PIL Imaging Library FileNotFoundError
Question:
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('Desktopexample.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:Python34libtkinter__init__.py", line 1533, in __call__
return self.func(*args)
File "C:UsersPamalDesktopDocumentsPython FolderPython [Learn]imaging_example.py", line 7, in showImg
load = Image.open('Desktopexample.jpg')
File "C:Python34libsite-packagesPILImage.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: ‘Desktopexample.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 😛
Does it have something to do with this line:
Answers:
From your traceback your script seems to be located in – C:UsersPamalDesktopDocumentsPython FolderPython [Learn]imaging_example.py
But you are trying to access something inside (Desktop folder) using relative path – Desktopexample.jpg
.
This would not work, unless your script is inside – C:UsersPamal
.
Better would be to give absolute path, such as – C:UsersPamalDesktopexample.jpg
.
Code –
load = Image.open(r'C:UsersPamalDesktopexample.jpg')
Updated code:
I seemed to have gotten this error resolved but i don’t understand how. I used a different image, in my pictures folder rather than the desktop and it seemed to work. Although i don’t understand why?? Does anyone notice a difference in my 1st code and this one below?
I had the same problem as you. I ran a program some months ago that works very well, but today it had the same error. What I did, which works for me, is change the directory to your home directory. For example, before, I loaded an image from a folder on my Desktop (i.e. ~/Desktop/images/example.png
), but I changed the directory to my Home environment (i.e. ~/home/pouyan/images/example.png
), and it worked without error.
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('Desktopexample.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:Python34libtkinter__init__.py", line 1533, in __call__
return self.func(*args)
File "C:UsersPamalDesktopDocumentsPython FolderPython [Learn]imaging_example.py", line 7, in showImg
load = Image.open('Desktopexample.jpg')
File "C:Python34libsite-packagesPILImage.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: ‘Desktopexample.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 😛
Does it have something to do with this line:
From your traceback your script seems to be located in – C:UsersPamalDesktopDocumentsPython FolderPython [Learn]imaging_example.py
But you are trying to access something inside (Desktop folder) using relative path – Desktopexample.jpg
.
This would not work, unless your script is inside – C:UsersPamal
.
Better would be to give absolute path, such as – C:UsersPamalDesktopexample.jpg
.
Code –
load = Image.open(r'C:UsersPamalDesktopexample.jpg')
Updated code:
I seemed to have gotten this error resolved but i don’t understand how. I used a different image, in my pictures folder rather than the desktop and it seemed to work. Although i don’t understand why?? Does anyone notice a difference in my 1st code and this one below?
I had the same problem as you. I ran a program some months ago that works very well, but today it had the same error. What I did, which works for me, is change the directory to your home directory. For example, before, I loaded an image from a folder on my Desktop (i.e. ~/Desktop/images/example.png
), but I changed the directory to my Home environment (i.e. ~/home/pouyan/images/example.png
), and it worked without error.