Re: Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-31 Thread Paul St George
That's what I wanted! But, I didn't know the question because I didn't 
know the answer.



On 30/05/2018 23:09, Karsten Hilbert wrote:

On Wed, May 30, 2018 at 11:01:17PM +0200, Peter J. Holzer wrote:


On 2018-05-30 22:08:45 +0200, Paul St George wrote:

Ha! No, my question was clumsy.

If I know the name of the viewer that I want to use (say for example:
‘ImageMagick’), where do I find the argument that should be used in a line
of code such as this:

ImageShow.register(MyViewer("gwenview"), -1)

$> man -k ImageMagick
$> man whatever_you_found_with_the_above

Karsten


--
Paul St George
http://www.paulstgeorge.com
http://www.devices-of-wonder.com

+44(0)7595 37 1302

--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-30 Thread Karsten Hilbert
On Wed, May 30, 2018 at 11:01:17PM +0200, Peter J. Holzer wrote:

> On 2018-05-30 22:08:45 +0200, Paul St George wrote:
> > Ha! No, my question was clumsy.
> > 
> > If I know the name of the viewer that I want to use (say for example:
> > ‘ImageMagick’), where do I find the argument that should be used in a line
> > of code such as this:
> > 
> > ImageShow.register(MyViewer("gwenview"), -1)

$> man -k ImageMagick
$> man whatever_you_found_with_the_above

Karsten
-- 
GPG key ID E4071346 @ eu.pool.sks-keyservers.net
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-30 Thread Peter J. Holzer
On 2018-05-30 22:08:45 +0200, Paul St George wrote:
> Ha! No, my question was clumsy.
> 
> If I know the name of the viewer that I want to use (say for example:
> ‘ImageMagick’), where do I find the argument that should be used in a line
> of code such as this:
> 
> ImageShow.register(MyViewer("gwenview"), -1)
> 
> I want to replace ‘gwenview’ with the name of my favourite viewer (for
> example: ‘ImageMagick’).

If your favourite viewer is 'ImageMagick’, then the name is
'ImageMagick'. However, I doubt this is the case, since ImageMagick
isn't a viewer, it is a collection of programs for manipulating images.
The viewer included in the ImageMagick package is simply called
'display' (and it is already the default viewer in PIL, so you don't
have to do anything to use it).

hp

-- 
   _  | Peter J. Holzer| we build much bigger, better disasters now
|_|_) || because we have much more sophisticated
| |   | h...@hjp.at | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson 


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-30 Thread Paul St George

Ha! No, my question was clumsy.

If I know the name of the viewer that I want to use (say for example: 
‘ImageMagick’), where do I find the argument that should be used in a 
line of code such as this:


ImageShow.register(MyViewer("gwenview"), -1)

I want to replace ‘gwenview’ with the name of my favourite viewer (for 
example: ‘ImageMagick’).




On 30/05/2018 02:31, Steven D'Aprano wrote:

On Tue, 29 May 2018 20:02:22 +0200, Paul St George wrote:


Is there, somewhere, a list of viewers and their names (for the purposes
of this script)?

Do you mean a list of programs capable of viewing graphics? Do you think
there is some sort of central authority that registers the names of all
such programs? *wink*


You can start here:

https://en.wikipedia.org/wiki/Category:Graphics_software

but that's probably the closest you're going to get.





--
Paul St George
http://www.paulstgeorge.com
http://www.devices-of-wonder.com

+44(0)7595 37 1302

--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-30 Thread Paul St George
True, but I wanted to have some control over the image window, 
fullscreen, colour depth, etc. I am also exploring pygame. I will try 
your suggestion as it is so much simpler.


Being a novice, I had been frightened off using shell=True. See 
.


Is this equivalent?
p = subprocess.Popen('display',  + imagepath)

so

p = subprocess.Popen('display',  'test.png')





On 30/05/2018 03:04, Ian Kelly wrote:

On Sat, May 26, 2018 at 9:17 AM, 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.

You probably shouldn't be using PIL at all then. Why open the file in
Python just to export it and re-open it in an image viewer? It would
be simpler just to point whichever image viewer you prefer at the
original file directly. Your entire script could just be something
like this:

import subprocess

# Some timing logic

subprocess.call("display " + imagepath, shell=True)



--
Paul St George
http://www.paulstgeorge.com
http://www.devices-of-wonder.com

+44(0)7595 37 1302

--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-29 Thread Ian Kelly
On Sat, May 26, 2018 at 9:17 AM, 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.

You probably shouldn't be using PIL at all then. Why open the file in
Python just to export it and re-open it in an image viewer? It would
be simpler just to point whichever image viewer you prefer at the
original file directly. Your entire script could just be something
like this:

import subprocess

# Some timing logic

subprocess.call("display " + imagepath, shell=True)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Re: Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-29 Thread Paul St George
Is there, somewhere, a list of viewers and their names (for the purposes 
of this script)?


I am assuming that if I want to ImageMagick (for example), there would 
be some shorter name - such as 'magick' - and it would be lower case .



On 29/05/2018 08:58, Peter Otten wrote:

Paul St George wrote:


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?

No, this tends to become a maintenance problem. Instead write a little
module of your own


from PIL import ImageShow

class MyViewer(ImageShow.UnixViewer):
 def __init__(self, command):
 self.command = command
 def get_command_ex(self, file, **options):
 return (self.command,) * 2

ImageShow.register(MyViewer("gwenview"), -1)


(replace "gwenview" with your favourite viewer) and import it before using
Image.show().




--
Paul St George
http://www.paulstgeorge.com
http://www.devices-of-wonder.com

+44(0)7595 37 1302

--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-29 Thread Paul St George

I tried this anyway. The error was:

    non-keyword arg after keyword arg



On 27/05/2018 21:51, Dennis Lee Bieber wrote:

On Sun, 27 May 2018 19:59:41 +0200, Paul St George 
declaimed the following:


So, on Unix I would use

Image.show(title=None, nameofdisplayutilty), or Image.show(title=None,
scriptname) #where script with name scriptname invokes the program

I will try this now! And thank you.


That is based upon my interpretation of the documentation... If the
other participant is right, however, then the "command" parameter may not
even be getting used at the lower levels, and only the list of registered
viewers is examined. In that case one would have to register a function to
invoke the viewer.




--
Paul St George
http://www.paulstgeorge.com
http://www.devices-of-wonder.com

+44(0)7595 37 1302

--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-29 Thread Paul St George

Thank you. For the advice, and for the new word 'monkeypatch'.


On 27/05/2018 23:58, Cameron Simpson wrote:

On 27May2018 20:15, Paul St George  wrote:

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?


It becomes a maintenance problem.

Alternatively you could:

Just write your own show function which accepts an Image and displays 
it with your program of choice. You might need to write some 
equivalent code which saves the Image to a file first, and removes it 
afterwards.


You could copy the show() code into a function of your own (i.e. in 
your own codebase) modify that to suit, then monkeypatch the class:


 Image.show = your_personal_show_function

when your programme starts. That way the code changes are not in the 
PIL code.


Cheers,
Cameron Simpson 



--
Paul St George
http://www.paulstgeorge.com
http://www.devices-of-wonder.com

+44(0)7595 37 1302

--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-29 Thread Paul St George

Should the PIL code be corrected?


On 28/05/2018 06:34, Christian Gollwitzer wrote:

Am 27.05.18 um 23:58 schrieb Cameron Simpson:

On 27May2018 20:15, Paul St George  wrote:

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?


It becomes a maintenance problem.

Alternatively you could:

Just write your own show function which accepts an Image and displays 
it with your program of choice. You might need to write some 
equivalent code which saves the Image to a file first, and removes it 
afterwards.


You could copy the show() code into a function of your own (i.e. in 
your own codebase) modify that to suit, then monkeypatch the class:


  Image.show = your_personal_show_function

when your programme starts. That way the code changes are not in the 
PIL code.


I think this is a bug/misfeature in the PIL code. On all 3 major 
platforms there is a way to invoke the standard program for a given 
file or URL. On Windows, it is "cmd.exe /c start ...", on OSX it is 
"open " and on Linux it is "xdg-open ...". That way the file is 
opened by whatever the user has set in his desktop environment.


Technically, xdg-open needs not to be present on Linux, though it is 
usually installed.


Christian



--
Paul St George
http://www.paulstgeorge.com
http://www.devices-of-wonder.com

+44(0)7595 37 1302

--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-29 Thread Peter Otten
Paul St George wrote:

> 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?

No, this tends to become a maintenance problem. Instead write a little 
module of your own


from PIL import ImageShow

class MyViewer(ImageShow.UnixViewer):
def __init__(self, command):
self.command = command
def get_command_ex(self, file, **options):
return (self.command,) * 2

ImageShow.register(MyViewer("gwenview"), -1)


(replace "gwenview" with your favourite viewer) and import it before using 
Image.show().

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-27 Thread Paul St George

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
,
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

--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-27 Thread Paul St George

So, on Unix I would use

Image.show(title=None, nameofdisplayutilty), or Image.show(title=None, 
scriptname) #where script with name scriptname invokes the program


I will try this now! And thank you.


On 26/05/2018 19:30, Dennis Lee Bieber wrote:

On Sat, 26 May 2018 17:17:42 +0200, Paul St George 
declaimed the following:


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
,
but I am none the wiser.


AS the examples ("On OS-of-choice...") illustrate... command is any
external executable program that can handle the temporary file, I presume
using the name of the temporary and not via redirection of stdin... If you
have a hex-editor, you could even supply that as "command". If you need to
provide more than the program name, you may have to instead create a
shell-script, set it executable, and pass the script name -- have the
script then invoke the program with the options and forwarding the
temporary file name to it.




--
Paul St George
http://www.paulstgeorge.com
http://www.devices-of-wonder.com

+44(0)7595 37 1302

--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-26 Thread Peter Otten
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
>  #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


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Re: The PIL show() method looks for the default viewer. How do I change this to a different viewer (of my choice)?

2018-05-26 Thread Paul St George

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 
, 
but I am none the wiser.



On 26/05/2018 01:02, boB Stepp wrote:

On Fri, May 25, 2018 at 6:04 AM, Paul St George  wrote:

I am using the Python Imaging Library (PIL), Python 2 and Raspberry Pi 3 B+

My code is simply:

 from PIL import Image

 im = Image.open(‘somepic.jpg’)
 im.show() # display image


But the show() method looks for the default viewer (probably xv). How do I
change this (in the code, or in some settings) to a different viewer (of my
choice)?

In the PIL docs for show() at
https://pillow.readthedocs.io/en/5.1.x/reference/Image.html#PIL.Image.Image.show
it says:


Image.show(title=None, command=None)

Displays this image. This method is mainly intended for debugging purposes.

On Unix platforms, this method saves the image to a temporary PPM
file, and calls either the xv utility or the display utility,
depending on which one can be found.

On macOS, this method saves the image to a temporary BMP file, and
opens it with the native Preview application.

On Windows, it saves the image to a temporary BMP file, and uses the
standard BMP display utility to show it (usually Paint).

Parameters:

title – Optional title to use for the image window, where possible.
command – command used to show the image


I have not had occasion to use PIL, but perhaps this command parameter
can be used to do what you want?  If not then perhaps you can write
your own function to load your image into your favorite image viewer.




--
https://mail.python.org/mailman/listinfo/python-list