python Image模块调用show()异常问题

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的查看器关闭才认为当前的语句执行结束,执行结束后,马上删除临时文件。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值