ubuntu系统
ubuntu15.10系统,python2.7(version)
import Image
im = Image.open(‘1.jpg’)
im.show()
运行后,无任何显示,也没有提示错误,im.size等属性显示正常。
问题解析:
Displays an image. This method is mainly intended for debugging purposes.
On Unix platforms, this method saves the image to a temporary PPM file, and calls the xv utility.
On Windows, it saves the image to a temporary BMP file, and uses the standard BMP display utility to show it.
调用Python的标准图像库里show()函数,无法显示图片。
因为它默认调用了外部的xv查看器,但xv并未被安装。
解决办法:
先装一个ImageMagic,就相当于把xv的入口换成了ImageMagic的display。
sudo apt-get install imagemagick
win7系统
在win7下也遇到了类似的问题
就像上面说的,windows系统会为要显示的图像创建一个临时的bmp图像,然后调用bmp查看器去读取图像。
参考解决方案:http://www.thecodingforums.com/threads/python-pil-and-vista-windows-7-show-not-working.707158/
What actually happens is that the Windows code relies on the fact that the default image viewer on Windows XP was able to work at blocking mode - this means that the command will wait until the image window will be closed. Because of that behaviour, they constructed a command line which deletes a file right after the image has been shown.
Windows Vista doesn't work the same way, it immediately returns once the command has been executed, so what happens now is that the temporary file gets immediately deleted by the command line, and the image viewer doesn't have enough time to load the image before it is being deleted.
Here is a quick workaround:
Edit C:\Python26\lib\site-packages\PIL\ImageShow.py, and around line 99, replace with the following line:
return "start /wait %s && PING 127.0.0.1 -n 5 > NUL && del /f %s" % (file, file)
这里解释为:
在win7下,语句会迅速执行,并返回结果,执行结束后,临时文件马上被删除,导致bmp的查看器无法读取文件;
win xp能够正常运行是因为xp的阻塞机制,xp下执行会让等到bmp的查看器关闭才认为当前的语句执行结束,执行结束后,马上删除临时文件。