OPEN IMAGE USING PYTHON showing error

2019-06-09 18:49发布

I am trying to open an image using python; I wrote the following code :

from PIL import Image
im=Image.open("IMG_1930.jpg")
im.show()

But the windows photo viewer opens but it shows the following message instead of the photos:

"windows photo viewer can not open this picture because either the picture is deleted , or it isn't in a location that is accessible."

2条回答
Root(大扎)
2楼-- · 2019-06-09 19:13

The show method in PIL is a poor's man way of viewing an image - it has got a hardcoded image viewer application, and writes your image data to a temporary file before calling that as an external application.

What is happening there is that you are either having problems with Windows' uneven access rights policies, and the viewer can't open the file in Python's temporary directory, or there is a problem with Window's problematic path specifications - it might even be a bug in PIL, that renders the temporary paht generated by PIL unusable by the image viewer.

If you are using show in a windowing application, use your tookit's way of viewing images to display it instead - otherwise, if it is a simpler application, build up a Tkitner Window and put the image in it, instead of show.

import sys

import Tkinter
from PIL import Image, ImageTk

window = Tkinter.Tk()
img = Image.open("bla.png")
img.load()
photoimg = ImageTk.PhotoImage(img)
container = Tkinter.Label(window, image=photoimg)
container.pack()

Tkinter.mainloop()

(Linux users: some distributions require the separate install of Tkinter support for PIL/PILLOW. In Fedora, for example, one has to install the python-pillow-tk package )

查看更多
来,给爷笑一个
3楼-- · 2019-06-09 19:21

I also had problems with this. Take a look at this post it fixed my problem: PIL image show() doesn't work on windows 7

Good luck fixing it.

查看更多
登录 后发表回答