PIL's Image.show() brings up *two* different v

2019-07-11 04:49发布

When toying with images in the python shell, I use image.show(), where image is an instance of Image. Long ago nothing happened, but after defining a symlink to mirage named "xv", I was happy.

The last few days, show() will bring up both ImageMagick's display and also Mirage. It's not clear where show() gets information on what to run. Documentation wasn't helpful. How to make it behave and bring up only what it thinks is xv?

6条回答
Juvenile、少年°
2楼-- · 2019-07-11 05:24

Well, for one thing, im.show is only intended for debugging purpose, it isn't guaranteed to work.

Nevertheless, you can always look at the source (open "pydoc PIL", the FILE section points out where a module is located):

In Windows, PIL will use "start /wait filename"

In Macs, it uses "open -a /Applications/Preview.app"

and on Linux, either 'display' if found or otherwise 'xdg-open'.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-07-11 05:34

ImageShow has a list of supported and available viewers in ImageShow._viewers. On my box, two viewers are found:

>>> import PIL.ImageShow
>>> PIL.ImageShow._viewers
[<PIL.ImageShow.DisplayViewer instance at 0x2587ef0>, <PIL.ImageShow.XVViewer instance at 0x2587f80>]

Unfortunately ImageShow.show() uses all available viewers. Thus two windows are displayed.

A solution is to reduce the viewer registry to only the first available viewer:

>>> PIL.ImageShow._viewers = [ PIL.ImageShow._viewers[0] ]
查看更多
Luminary・发光体
4楼-- · 2019-07-11 05:39

A sort of a workaround I found.

I used:

image.save('something.png')

Opened the png from the file manager using the default preview program. Then every time I called save again, the previewer automatically refreshed, and I got the new image :)

查看更多
闹够了就滚
5楼-- · 2019-07-11 05:40

it is able to specify the viewer as command argument to the show method, e.g.

img.show(command='feh')

查看更多
Fickle 薄情
6楼-- · 2019-07-11 05:41

I encountered the same problem, it is a problem in PIL. You can change the PIL code in the following way:

In the file ImageShow.py replace

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

with

return "start /wait %s && del /wait /f %s" % (file,file)
查看更多
放荡不羁爱自由
7楼-- · 2019-07-11 05:49

A little out-off-dated but... I solved this problem changing the code of file /usr/lib/python2.7/dist-packages/PIL/ImageShow.py. Is missing a return on method show of Viewer class (near line 66): return self.show_image(image, **options).

查看更多
登录 后发表回答