Python PIL and Vista/Windows 7 .. show() not working ...

E

Esmail

Hello all.

I am using the PIL 1.1.6 and Python 2.6.x under XP without any
problems. However, I can't display any images under Vista
or Windows 7. I could understand Windows 7 as it's relatively
new, but Vista has been around for a bit.

Sample code:

import Image

im = Image.open('c://mypic.jpg')
im.show()


this will work fine under XP, but under Windows 7 and Vista
the default image viewer will come up with some error message
that the image can't be found.

I tried with an external image view program and tried to supply
it via the command parameter to show - but that too didn't work.

Definition: im.show(self, title=None, command=None)

Any suggestions/help/workarounds? If you can get this to work
with Vista or Windows 7 I'd love to hear from you.

Thanks!
Esmail
 
E

Esmail

  im = Image.open('c://mypic.jpg')

sorry, slip of the finger, there's only one forward slash
or you can use two backward slashes.

The problem isn't with opening it (I know it opens fine
since I can get its size attribute via im.size) - the show()
is the problem.

Esmail
 
L

Lie Ryan

sorry, slip of the finger, there's only one forward slash
or you can use two backward slashes.

The problem isn't with opening it (I know it opens fine
since I can get its size attribute via im.size) - the show()
is the problem.


What's your default image viewer? im.show is intended to be for
debugging purpose and may always guaranteed to work if your image viewer
doesn't support receiving the file through <I don't know how PIL passes
the image to the program>.
 
E

Esmail

What's your default image viewer? im.show is intended to be for
debugging purpose and may always guaranteed to work if your image viewer
doesn't support receiving the file through <I don't know how PIL passes
the image to the program>.


It's whatever the default windows viewer is :) .. so if I double-
click on
the image in the filemanager it fires it up and shows it. This works
in XP and Windows 7 and Vista (ie double clicking on the image and
having it display).

I dug around in the docs and found a named parameter that I can set
when I
call show.

Definition: im.show(self, title=None, command=None)

I installed irfanview and specified it/its path in the parameter,
but that didn't work either. It's really quite puzzling in the
case of Vista since that's been around for quite a few years now.


Esmail
 
D

David Bolen

Esmail said:
I dug around in the docs and found a named parameter that I can set
when I
call show.

Definition: im.show(self, title=None, command=None)

I installed irfanview and specified it/its path in the parameter,
but that didn't work either. It's really quite puzzling in the
case of Vista since that's been around for quite a few years now.

But I thought everyone was sticking their fingers in their ears and
humming to try to forget Vista had been released, particularly now
that Windows 7 is out :)

Perhaps there's an issue with the temporary file location. I don't
have a Vista system to test on, but the show() operation writes the
image to a temporary file as returned by tempfile.mktemp(), and then
passes the name on to the external viewer. The viewing command is
handed to os.system() with the filename embedded without any special
quoting. So if, for example, the temporary location has spaces or
"interesting" characters, it probably won't get parsed properly.

One easy debugging step is probably to add a print just before the
os.system() call that views the image (bottom of _showxv function in
Image.py in my copy of 1.1.6). That way at least you'll know the
exact command being used.

If that's the issue, there are various ways around it. You could
patch PIL itself (same function) to quote the filename when it is
constructing the command. Alternatively, the tempfile module has a
tempdir global you could set to some other temporary directory before
using the show() function (or any other code using tempfile).

-- David
 
E

Esmail

But I thought everyone was sticking their fingers in their ears and
humming to try to forget Vista had been released, particularly now
that Windows 7 is out :)

Perhaps there's an issue with the temporary file location.  I don't
have a Vista system to test on, but the show() operation writes the
image to a temporary file as returned by tempfile.mktemp(), and then
passes the name on to the external viewer.  The viewing command is
handed to os.system() with the filename embedded without any special
quoting.  So if, for example, the temporary location has spaces or
"interesting" characters, it probably won't get parsed properly.

One easy debugging step is probably to add a print just before the
os.system() call that views the image (bottom of _showxv function in
Image.py in my copy of 1.1.6).  That way at least you'll know the
exact command being used.

If that's the issue, there are various ways around it.  You could
patch PIL itself (same function) to quote the filename when it is
constructing the command.  Alternatively, the tempfile module has a
tempdir global you could set to some other temporary directory before
using the show() function (or any other code using tempfile).

-- David

Thanks for the pointers David, this will give me some things to
investigate.
As for me, I'm a long time and regular Linux user with some XP tossed
in.
I use the PIL under XP at times, but this problem is happening to
someone
I know who is using both Vista and Windows and can't get the basic
thing
to work so I am trying to help. (I have access to a Win 7 VM for
testing
purposes at least).

If I find a work-around or fix or concrete cause I'll post. In the
meantime
if anyone has any other ideas or fixes/suggestions, please don't be
shy :)

Thanks,
Esmail
 
T

Terry Reedy

Esmail said:
Thanks for the pointers David, this will give me some things to
investigate.
As for me, I'm a long time and regular Linux user with some XP tossed
in.
I use the PIL under XP at times, but this problem is happening to
someone
I know who is using both Vista and Windows and can't get the basic
thing
to work so I am trying to help. (I have access to a Win 7 VM for
testing
purposes at least).

If I find a work-around or fix or concrete cause I'll post. In the
meantime
if anyone has any other ideas or fixes/suggestions, please don't be
shy :)

How about forget PIL's show and its automagic behavior.
Use PIL's image save to write the image to a nice, known location, such
as C:/temp/pic.jgp -- nothing but alphanumerics + ':/.'
Next test how to run a known external viewer from a command window.
Then use os.system or subprocess to run it with the same command line.
Package the two lines as a myshow(params) function.
 
Joined
Mar 12, 2010
Messages
1
Reaction score
1
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)
 
Joined
Aug 19, 2010
Messages
1
Reaction score
0
Hey, just wanted to say thanks. I found this via google, finally I can use .show() again.
Would it be a good idea to post this as bug/fix to the PIL developer's board?
 
Joined
Aug 16, 2014
Messages
1
Reaction score
0
Hello

It works for me too, but I was wondering if I need to compile the file, o just save it with changes.

thanks in advance, Jaime

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top