[python-win32] PIL issue with desktop wallpaper changing

Chris Stromberger chris.stromberger at gmail.com
Thu Jan 27 00:33:40 CET 2005


I have a script to change the desktop wallpaper.  If it encounters a
corrupt jpg, it seems to get off track and not recover.  Here's the
code snippet.  This is called in a loop with several jpg images in a
directory to switch the wallpaper.

im = Image.open(filename)
[call im.resize() here if necessary to reduce/enlarge image size to fill screen]
im.save('wallpaper.bmp')
cs = ctypes.c_buffer('wallpaper.bmp')
ok = ctypes.windll.user32.SystemParametersInfoA(win32con.SPI_SETDESKWALLPAPER,
0, cs, 0)

The above fails in the im.save call when passed a corrupt jpg--an
exception is raised in im.save.  I catch it and loop to the next
image.  The next time this block is called, and for every call
thereafter, the SystemParametersInfoA call fails (returns 0), and if I
call GetLastError() after that, it shows error 32, which is a file
sharing violation.  I added an os.unlink('wallpaper.bmp') to the start
of this block and it works until the corrupt jpg is processed. 
Thereafter, the file cannot be deleted ([Errno 13] Permission denied).
 So it seems like something is grabbing onto the file, but I have no
idea how to release that handle or what it is.

One further clue: if I raise an exception (anything) after detecting
that ok is 0, then the next image I try to load will work.  The
exception raising clears everything out.

So here are the two scenarios:

0. call with normal image(s)--works, wallpaper is changed each time
1. call with corrupt jpg--exception.
2. then call with normal images--SystemParametersInfoA returns 0 every
time, wallpaper not changed again.

if I add...
3. raise exception if SystemParametersInfoA returns 0--the next and
subsequent images work.  Until we loop around to the corrupt jpg
again.

Any ideas what's going on?

Thanks,
Chris

http://www.fetidcascade.com/


More information about the Python-win32 mailing list