python 2.x - How to paste chunk of images one after another memory-efficiently? -
i'm processing several hundred images cutting them automatically @ height , pasting chunks 1 after other. create new image instance new taller size last time in order make space new chunk. when processioning done, final result saved file.
my problem right approach consumes lot of ram , swap, freezing computer. there way achieve objective without consuming lot of ram? far, code have:
import simplecv cv import argparse import pil os.path import join, abspath parser = argparse.argumentparser( description=("process many images , paste chunks final image")) # ...code removed brevity... argv = parser.parse_args() rango_inicio = int(argv.rango.split(":")[0]) rango_fin = int(argv.rango.split(":")[1]) img = none pag in xrange(rango_inicio, rango_fin + 1): numero = format(pag, '0' + argv.relleno) pagina = join( argv.dir_entrada, argv.prefijo + numero + '.' + argv.extension) pagina = abspath(pagina) print(pagina) imagen = cv.image(pagina) fs = sorted(imagen.findlines(), key=lambda f: f.width())[-1] if fs.width() >= 598: cropped = imagen.crop(0, fs.y, imagen.width, imagen.height) if not img: img = pil.image.new("rgb", (cropped.width, cropped.height)) croppedraw = cropped.getpil() img.paste(croppedraw, (0, 0)) else: croppedraw = cropped.getpil() imgtmp = img.copy() img = pil.image.new( "rgb", (imgtmp.size[0], imgtmp.size[1] + cropped.height)) img.paste(imgtmp, (0, 0)) img.paste(croppedraw, (0, imgtmp.size[1])) # , save final image
you first loading image in cv, making list of each row in image list, apparently find it's width, crop it, , make pil image out of it.
i think can of in pil directly, means need 1 copy of file in memory, making better.
that said, shouldn't start swap anyway, unless have huge images, each image should garbage collected once have loaded new image memory, there might issue involved.
Comments
Post a Comment