简体   繁体   English

PIL图像show()在Windows 7上不起作用

[英]PIL image show() doesn't work on windows 7

I would like to show an image using python on windows and other platforms. 我想在Windows和其他平台上使用python显示图像。 When I do: 当我做:

from PIL import Image
im = Image.open('image.png')
im.show()

my default viewer opens up and tells me that Windows Photo Viewer can't open this picture because either this file was deleted , etc. 我的默认查看器打开并告诉我Windows Photo Viewer can't open this picture because either this file was deleted等。

The file is probably deleted because PIL calls the os with the following command: "start /wait %s && del /f %s" % (file, file) 该文件可能已删除,因为PIL使用以下命令调用操作系统: "start /wait %s && del /f %s" % (file, file)

I found a workaround here . 我在这里找到了解决方法。 They recommend changing PIL's code to "start /wait %s && PING 127.0.0.1 -n 5 > NUL && del /f %s" % (file, file) . 他们建议将PIL的代码改为"start /wait %s && PING 127.0.0.1 -n 5 > NUL && del /f %s" % (file, file) However, I want others to be able to use my code. 但是,我希望其他人能够使用我的代码。

Is there a simple solution? 有简单的解决方案吗? Should I look for an alternative to PIL that would work crossplatform? 我应该寻找可以在跨平台上工作的PIL的替代方案吗?

Ok, found a solution here : 好的,在这里找到了解决方案:

import webbrowser
webbrowser.open('image.png')

It opens the default viewer, not the browser, on my machine. 它在我的机器上打开默认查看器,而不是浏览器。

Also, there is os.startfile . 另外,还有os.startfile

If you want it to be opened by MS paint only, you can use: 如果您只想通过MS paint打开它,您可以使用:

start /wait mspaint %s && del /f %s" % (file, file)

The /wait is not having any effect when the file name is specified directly. 直接指定文件名时, /wait没有任何影响。

When Photo Viewer does appear, Go to menu on top ->Open->Choice Program-> Select Paint 当Photo Viewer出现时,转到顶部的菜单 - >打开 - >选择程序 - >选择绘画
On Spanish -> Abrir-> Elegir Programa -> seleccionar Paint 西班牙语 - > Abrir-> Elegir Programa - > seleccionar Paint

image reference: image 图像参考: 图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM