使用PYTHON打开图像显示错误 [英] OPEN IMAGE USING PYTHON showing error

查看:73
本文介绍了使用PYTHON打开图像显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python打开图像;我写了以下代码:

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

但是Windows照片查看器会打开,但显示以下消息而不是照片:

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

"Windows照片查看器无法打开该图片,因为该图片已删除或不在可访问的位置."

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

推荐答案

PIL中的show方法是穷人查看图像的方式-它具有硬编码的图像查看器应用程序,并将您的图像数据写入到临时文件,然后再将其作为外部应用程序调用.

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.

发生的事情是您可能是Windows的访问权限策略不统一而导致问题,查看器无法在Python的临时目录中打开文件,或者Window的路径规范有问题-甚至可能是PIL中的一个错误,导致PIL生成的临时页面无法被图像查看器使用.

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.

如果您在窗口应用程序中使用show,请改用您的taket查看图像的方式来显示它-否则,如果它是一个更简单的应用程序,请构建一个Tkitner窗口并将其放在其中,而不是show.

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用户:某些发行版本需要单独安装Tkinter对PIL/PILLOW的支持.例如,在Fedora中,必须安装python-pillow-tk软件包)

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

这篇关于使用PYTHON打开图像显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆