The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?
Paul St George
email at paulstgeorge.com
Sun May 27 14:15:02 EDT 2018
This is very helpful indeed, thank you. Awe-inspiring.
It occurred to me that I could edit the PIL/ImageShow.py, replacing ‘xv’
(in five places) with the utility of my choice and using ‘executable’ as
the command.
Or, is this just not done?
On 26/05/2018 19:11, Peter Otten wrote:
> Paul St George wrote:
>
>> Thank you.
>> You are very right. The show() method is intended for debugging purposes
>> and is useful for that, but what method should I be using and is PIL the
>> best imaging library for my purposes? I do not want to manipulate
>> images, I only want to show images (full screen) on an external display.
>> I want to use Python to control the timing of the images.
>>
>> And, out of curiosity, as I will probably use a different method - how
>> do I find out what commands can be used as parameters for show()? I read
>> the docs at
>> <https://pillow.readthedocs.io/en/5.1.x/reference/Image.html
>> #PIL.Image.Image.show>,
>> but I am none the wiser.
> If you look into the source code
>
> https://github.com/python-pillow/Pillow/blob/master/src/PIL/Image.py#L1967
>
> after a few indirections
>
> show --> _show --> _showxv --> ImageShow.show
>
> you will end up in the file
>
> https://github.com/python-pillow/Pillow/blob/master/src/PIL/ImageShow.py
>
> which is short enough to read completely ;)
>
> At first glance it looks like the command argument is silently discarded, so
> here's plan B:
>
> It turns out that you can register() Viewer instances, and the pre-
> registered viewers for unixoids -- and thus probably the Pi are
> DisplayViewer and XVViewer.
>
> Using these as a template I came up with the following simple viewer that
> shows an image with firefox:
>
> #!/usr/bin/env python3
> import os
> import sys
>
> from PIL import Image, ImageShow
>
>
> class Firefox(ImageShow.UnixViewer):
>
> # store the image in a format understood by the browser
> format = "jpeg"
>
> def get_command_ex(self, file, **options):
> return ("firefox",) * 2
>
> def show_file(self, file, **options):
> quote = ImageShow.quote
>
> command, executable = self.get_command_ex(file, **options)
>
> # firefox returns immediately, so let's sleep a few seconds
> # to give it time to actually open the image file
> command = "(%s %s; sleep 10; rm -f %s)&" % (
> command, quote("file://" + file), quote(file)
> )
> os.system(command)
> return 1
>
>
> # the -1 means our viewer will be inserted before the others
> # and thus become the default
> ImageShow.register(Firefox(), -1)
>
>
> if __name__ == "__main__":
> try:
> file = sys.argv[1]
> except IndexError:
> print("Please provide an image file", file=sys.stderr)
> else:
> image = Image.open(file)
> image.show()
>
>
> Here's another, even simpler one:
>
> class Gwenview(ImageShow.UnixViewer):
> def get_command_ex(self, file, **options):
> return ("gwenview",) * 2
>
>
>
--
Paul St George
http://www.paulstgeorge.com
http://www.devices-of-wonder.com
+44(0)7595 37 1302
More information about the Python-list
mailing list