Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python PIL and Vista/Windows 7 .. show() not working ...

Reply
Thread Tools

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

 
 
Esmail
Guest
Posts: n/a
 
      11-30-2009
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

 
Reply With Quote
 
 
 
 
Esmail
Guest
Posts: n/a
 
      11-30-2009
>
> * 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
 
Reply With Quote
 
 
 
 
Lie Ryan
Guest
Posts: n/a
 
      11-30-2009
On 12/1/2009 5:04 AM, Esmail wrote:
>>
>> 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.



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>.
 
Reply With Quote
 
Esmail
Guest
Posts: n/a
 
      11-30-2009
On Nov 30, 3:08*pm, Lie Ryan <(E-Mail Removed)> wrote:
>
> 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
 
Reply With Quote
 
David Bolen
Guest
Posts: n/a
 
      11-30-2009
Esmail <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
Esmail
Guest
Posts: n/a
 
      11-30-2009
On Nov 30, 4:37*pm, David Bolen <(E-Mail Removed)> wrote:
> Esmail <(E-Mail Removed)> writes:
> > 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


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
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      11-30-2009
Esmail wrote:
> On Nov 30, 4:37 pm, David Bolen <(E-Mail Removed)> wrote:


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


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.

 
Reply With Quote
 
ronreiter ronreiter is offline
Junior Member
Join Date: Mar 2010
Posts: 1
 
      03-12-2010
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)
 
Reply With Quote
 
thkoe002 thkoe002 is offline
Junior Member
Join Date: Aug 2010
Posts: 1
 
      08-19-2010
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?
 
Reply With Quote
 
db85 db85 is offline
Junior Member
Join Date: Mar 2012
Posts: 1
 
      03-21-2012
Thanks! works perfectly
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PIL ImageChops.difference not working correctly(?) Jon Reyes Python 1 02-19-2013 06:28 AM
PIL show() not working for 2nd pic suresh.amritapuri Python 3 01-09-2010 07:52 PM
PIL: problem to convert an image array to PIL format Sverre Python 2 12-17-2009 04:33 PM
Re: Python PIL and Vista/Windows 7 .. show() not working ... David Robinow Python 0 12-01-2009 03:38 PM
[PIL] is there a downloadable docs for PIL Egor Bolonev Python 2 12-24-2004 11:05 AM



Advertisments