[Image-SIG] Converting images to palette mode

Gregor Kopka gregor at kopka.net
Wed Jan 28 00:29:49 CET 2009


Hello Lukas,

could be that you found a memleak somewhere in PIL.

Maybe try to add some /del() /to force cleanup of the images - i'm not 
that sure at the moment but i remember i somewhen have read something 
somewhere about the need to use del(image) to force a correct cleanup in 
a special case.
In case you're curious if i'm right try this version which cleans used 
stuff explicitly via del().

also while looking at it i'm not that sure why you don't check the 
extension by checking against /a[-4:] == ".png" /which i feel like a bit 
more readable compared to your if condition.
Do you want to stop your code from processing files with a "." in the 
name (like 'mypic 02.05.2007.png') ?

Gregor

def start_image_crush(bla, path, files):
    os.chdir(path)
    print '\nACTUAL DIRECTORY: ' + path + '\n'
    for a in files:
        # directory and file filter
        if string.find(a,'.') > 0 and string.split(a,'.')[1]=='png' and len(string.split(a,'.'))==2:
            print 'Trying '+ a
            im = Image.open(a)
            if im.mode!='P':
                print '.....processing'
                if im.mode=='RGBA':
                    print "Warning, converting alpha transparency to 1-bit transparency"
                    al = im.split()[3]
                    nal = Image.eval(al, quantize_and_invert)
                    del(al)
                    colors=255
                else:
                    nal = None
                    colors=256
                if im.mode!='RGB':
                    nim = im.convert('RGB')
                else:
                    nim = im
                del(im)
                try:
                    im=nim.convert('P', palette=Image.ADAPTIVE, colors=colors)
                except ValueError, e:
                    print 'Python memusage: ' + str( memusage() ) + ' !!!'
                    raise e
                del(nim)

                if nal:
                    im.paste(255,None,nal)
                    del(nal)
                im.save(string.split(a,'.')[0]+'.png',optimize=1, transparency=255)
            else: 
                print 'Image still converted. No processing !!!'
            del(im)


Lukas Orsin schrieb:
> Thanks for answers.
> I am sure no image is broken. If I run my program once again, 
> previously stopped image is processed with no error and no exception.
> I tested conversion a few times with many little images on two 
> mashines (both 32bit Win 2003 server) and it crashed after allocation 
> 50 - 60 MB of memory.
> A preliminary test on 64bit Win 2008 server ended well with 200,000 
> converted images at memory allocation  250 MB in the end.
> We think problem takes about memory allocation at mass conversion.
>
> My script is attached.
> (usage: python to8bit_folders_upgrade.py c:\temp\images)
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Image-SIG maillist  -  Image-SIG at python.org
> http://mail.python.org/mailman/listinfo/image-sig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/image-sig/attachments/20090128/3cbfe5a9/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gregor.vcf
Type: text/x-vcard
Size: 314 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/image-sig/attachments/20090128/3cbfe5a9/attachment.vcf>


More information about the Image-SIG mailing list