Re: PIL show() not working for 2nd pic

Gregor Kopka <[email protected]>
Newsgroups gmane.comp.python.image
Message-ID <[email protected]>
Hello Suresh,

under windows image.show() blocks until the external viewer terminates 
(this is true at least with default installed viewer that comes with 
XP), after that the temporary file created by PIL will be unlinked.

I have no clue if this is true with gnome under Linux, but in case the 
call to open the external viewer returns immediately (before the viewer 
has loaded the image) you would see strange things happen since 
image.show() cleans up the temp file when the python script ends, so you 
likely run into a race condition: the temp file (created by 
im35x.show()) gets unlinked before the viewer had time to access it.

Always remember: In case you use code whith cleanup functionality (eg. 
removing temp files on shutdown like image.show() does) keep your code 
running long enough so the external programs you launched have a chance 
to access the files you want to hand over to them!

My best guess would be that

import time
time.sleep(10)

fixes your issue.

Some things i suggest to change in the workaround code from Jermoes: 
don't hardcode the filename and the location of the temp directory!

path = "/home/adys/.cache/%s.png" % "tmpVfBc3s"

will probably lead to problems down the road: eg. run two instances of 
the code in parallel and they'll fight over the temp file, switch to a 
viewer with a wrapper which immediately returns and you'll potentially 
overwrite the file again in case you open two viewers in quick 
succession, transfer the code to a different system where the hardcoded 
path dosn't exist and the save will fail, (etc.)

Better use:

def show(self):
    import os, tempfile
    path = os.path.join (tempfile.gettempdir(), "%s_%s.png" % (os.getpid(), id(self))
    self.save(path)
    os.popen("eog %s" % path)

to avoid the potential problems with hardcoded filenames.

Still two things to keep in mind with this:
- don't forget that with this introduces a dependency on eye of gnome
- don't forget to clean out the directory supplied by 
tempfile.gettempdir() from time to time to keep the disk from filling up

I hope this helps to shed some light on the subject.

Regards,

Gregor

Suresh Kumar schrieb:
> Thanks. Just now i modified all my show methods to
> im.show(command="display") and installed ImageMagick package. That
> temporarily solved my problem. Ok your solution is better anyway.
>
> suresh
>
>
> On Fri, Jan 8, 2010 at 1:15 PM, Jerome Leclanche <[email protected]> wrote:
>   
>> You can write a custom show method to your plugins:
>> def show(self):
>> path = "/home/adys/.cache/%s.png" % "tmpVfBc3s"
>> self.save(path)
>> import os
>> os.popen("eog %s" % path)
>> J. Leclanche / Adys
>>
>>
>> On Fri, Jan 8, 2010 at 11:08 PM, Suresh Kumar <[email protected]>
>> wrote:
>>     
>>> Hi
>>>
>>> Thanks. Though not mentioned in the PIL 1.1.6 documentation, I see
>>> from (sys.path)/PIL/Image.py that it can be done. But I have just one
>>> more qn. Is it possible to modify _showxv() without altering the
>>> original code of PIL ?
>>>
>>> suresh
>>>
>>> On Fri, Jan 8, 2010 at 1:00 PM, Jerome Leclanche <[email protected]>
>>> wrote:
>>>       
>>>> In recent versions of PIL you can do img.show(command="display %s") or
>>>> something similar (maybe without %s). Otherwise, edit _showxv in
>>>> (sys.path)/PIL/Image.py
>>>> J. Leclanche / Adys
>>>>
>>>>
>>>> On Fri, Jan 8, 2010 at 10:49 PM, Suresh Kumar
>>>> <[email protected]>
>>>> wrote:
>>>>         
>>>>> Hi,
>>>>>
>>>>> How do you do that? Can you elaborate a bit further?
>>>>>
>>>>> suresh
>>>>>
>>>>>
>>>>> On Fri, Jan 8, 2010 at 12:39 PM, Jerome Leclanche <[email protected]>
>>>>> wrote:
>>>>>           
>>>>>> I'm pretty sure it's a bug in Eye of Gnome. Have you tried hardcoding
>>>>>> another program in PIL/Image.py ? (_showxv, iirc)
>>>>>> J. Leclanche / Adys
>>>>>>
>>>>>>
>>>>>> On Fri, Jan 8, 2010 at 10:18 PM, Suresh Kumar
>>>>>> <[email protected]>
>>>>>> wrote:
>>>>>>             
>>>>>>> Hi
>>>>>>>
>>>>>>> Thanks for the reply.
>>>>>>>
>>>>>>> With one file, it is working correctly. Now I get the following
>>>>>>> error
>>>>>>> messages which are different from earlier "file not found ones".
>>>>>>>
>>>>>>> (eog:8368): Gtk-CRITICAL **: gtk_tree_model_get_iter: assertion
>>>>>>> `path->depth > 0' failed
>>>>>>>
>>>>>>> (eog:8368): Gtk-CRITICAL **: gtk_list_store_get_value: assertion
>>>>>>> `VALID_ITER (iter, list_store)' failed
>>>>>>>
>>>>>>> (eog:8368): GLib-GObject-WARNING **:
>>>>>>> /build/buildd/glib2.0-2.20.1/gobject/gtype.c:3940: type id `0' is
>>>>>>> invalid
>>>>>>>
>>>>>>> (eog:8368): GLib-GObject-WARNING **: can't peek value table for type
>>>>>>> `<invalid>' which is not currently referenced
>>>>>>> Segmentation fault
>>>>>>>
>>>>>>> So any suggestions?
>>>>>>> my code:
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> -----------------------------------------------------------------------------------------
>>>>>>> #!/usr/bin/python
>>>>>>> print "Aum Amriteshwaryai Namaha"
>>>>>>>
>>>>>>> import Image
>>>>>>>
>>>>>>> imagePath = "/home/suresh/EE241/book_images_3ed/ch03/"
>>>>>>>
>>>>>>> im34 = Image.open(imagePath + "breast_digital_Xray.tif")
>>>>>>> im35 = Image.open(imagePath + "DFT_no_log.tif")
>>>>>>> im35.show()
>>>>>>>
>>>>>>> def neg(x):
>>>>>>>    return 255-1-x
>>>>>>>
>>>>>>> import math
>>>>>>>
>>>>>>> def logtr(x):
>>>>>>>    y = math.log(1+x,10)
>>>>>>>    print y
>>>>>>>    return y*100
>>>>>>>
>>>>>>> im34x = im34.point(neg)
>>>>>>> im34x.show()
>>>>>>>
>>>>>>> im35x = im35.point(logtr)
>>>>>>> im35x.show()
>>>>>>>
>>>>>>> ----------------------------------------------end of
>>>>>>> code--------------------------------
>>>>>>> suresh
>>>>>>>
>>>>>>>
>>>>>>> On Fri, Jan 8, 2010 at 5:29 AM, Bram Mertens
>>>>>>> <[email protected]>
>>>>>>> wrote:
>>>>>>>               
>>>>>>>> On Fri, Jan 8, 2010 at 1:14 AM, suresh.amritapuri
>>>>>>>> <[email protected]> wrote:
>>>>>>>>                 
>>>>>>>>> Hi
>>>>>>>>>
>>>>>>>>> I am using PIL for image processing in ubuntu 9.04. When i give
>>>>>>>>> two
>>>>>>>>> im.show() commands for two different images, the second image is
>>>>>>>>> not
>>>>>>>>> displayed (eye of gnome is the display program). It says no such
>>>>>>>>> file
>>>>>>>>> or directory. Any ideas?
>>>>>>>>>                   
>>>>>>>> Have you verified that the path to the second image is correct and
>>>>>>>> that the image is readable by your script?
>>>>>>>>
>>>>>>>> e.g. try reversing the order of the images to identify whether or
>>>>>>>> not
>>>>>>>> it is the image that can not be found or eog.
>>>>>>>>
>>>>>>>> Another approach might be to check the file using the os module or
>>>>>>>> something similar.
>>>>>>>>
>>>>>>>> Regards
>>>>>>>>
>>>>>>>> Bram
>>>>>>>>
>>>>>>>>                 
>>>>>>>
>>>>>>> --
>>>>>>> R Suresh Kumar,
>>>>>>> Phd Student, Vislab
>>>>>>> EE, Univ. of California
>>>>>>> Riverside, CA 92507
>>>>>>> --
>>>>>>> Even after all this time The sun never says to the earth, "You owe
>>>>>>> Me."
>>>>>>> Look what happens with A love like that, It lights the Whole Sky.
>>>>>>> - Hafiz e Shirazi
>>>>>>> _______________________________________________
>>>>>>> Image-SIG maillist  -  [email protected]
>>>>>>> http://mail.python.org/mailman/listinfo/image-sig
>>>>>>>               
>>>>>>             
>>>>>
>>>>> --
>>>>> R Suresh Kumar,
>>>>> Phd Student, Vislab
>>>>> EE, Univ. of California
>>>>> Riverside, CA 92507
>>>>> --
>>>>> Even after all this time The sun never says to the earth, "You owe Me."
>>>>> Look what happens with A love like that, It lights the Whole Sky.
>>>>> - Hafiz e Shirazi
>>>>>           
>>>>         
>>>
>>> --
>>> R Suresh Kumar,
>>> Phd Student, Vislab
>>> EE, Univ. of California
>>> Riverside, CA 92507
>>> --
>>> Even after all this time The sun never says to the earth, "You owe Me."
>>> Look what happens with A love like that, It lights the Whole Sky.
>>> - Hafiz e Shirazi
>>>       
>>     
>
>
>
>

_______________________________________________
Image-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/image-sig
gregor.vcf (text/x-vcard, 314 B)
begin:vcard
fn:Gregor Kopka
n:Kopka;Gregor
org:Kopka.Net;Internet Services
adr:;;Grosse Strasse 10;Ahrensburg;;22926;Deutschland
email;internet:[email protected]
title:Inhaber
tel;work:+49 4102 823212
tel;fax:+49 4102 823213
tel;cell:+49 174 2550492
x-mozilla-html:FALSE
url:http://kopka.net
version:2.1
end:vcard
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.