0
我正在處理數百個圖像,通過在特定高度自動切割並依次粘貼大塊圖像。我創建了一個新的Image
實例,其中new
的尺寸比上一次更大,以便爲新塊創建空間。處理完成後,最終結果將保存到文件中。如何一次又一次地將大塊圖像粘貼到內存中?
我現在的問題是,這種方法是消耗大量的RAM和交換,幾乎凍結我的電腦。有沒有一種方法來實現我的目標,而不消耗大量的RAM?到目前爲止,這是我的代碼有:
import SimpleCV as cv
import argparse
import PIL
from os.path import join, abspath
parser = argparse.ArgumentParser(
description=("Process many images and paste the chunks into a final image"))
# ...code removed for brevity...
argv = parser.parse_args()
rango_inicio = int(argv.rango.split(":")[0])
rango_fin = int(argv.rango.split(":")[1])
img = None
for 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]))
# and save the final image