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)
import Image
import random, optparse
import sys, string, os, win32process

def quantize_and_invert(alpha):
    if alpha <= 64: # You may change the threshold value into something other.
        return 255
    return 0

def memusage():
    current_process = win32process.GetCurrentProcess()
    memory_info = win32process.GetProcessMemoryInfo(current_process)
    return memory_info["WorkingSetSize"] 

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]
                    al = Image.eval(al, quantize_and_invert)
                    colors=255
                else:
                    al = None
                    colors=256
                if im.mode!='RGB':
                    im = im.convert('RGB')
                try:
                    im=im.convert('P', palette=Image.ADAPTIVE, colors=colors)
                except ValueError, e:
                    print 'Python memusage: ' + str( memusage() ) + ' !!!'
                    raise e
                
                if al: im.paste(255,None,al)
                im.save(string.split(a,'.')[0]+'.png',optimize=1, transparency=255)
            else: 
                print 'Image still converted. No processing !!!'
            
p=string.replace(sys.argv[1],'\\','\\\\')
os.path.walk(p,start_image_crush, 'blablabla')
print '\nFINISH.'
_______________________________________________
Image-SIG maillist  -  Image-SIG@python.org
http://mail.python.org/mailman/listinfo/image-sig

Reply via email to