综合编程

Python PIL Imaging Library FileNotFoundError

微信扫一扫,分享到朋友圈

Python PIL Imaging Library FileNotFoundError

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 :P

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')

Getting Started with End-to-End Speech Translation

上一篇

new header, who dis

下一篇

你也可能喜欢

评论已经被关闭。

插入图片

热门栏目

Python PIL Imaging Library FileNotFoundError

长按储存图像,分享给朋友