Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating very large images using Python Image Library

I'm trying to create a very large image (25000x25000) by pasting together many smaller images. Upon calling Image.new() with such large dimensions, python runs out of memory and I get a MemoryError.

Is there a way to write out an image like this incrementally, without having the whole thing resident in RAM?

EDIT: Using ImageMagick's montage command, it seems possible to create arbitrarily sized images. It looks like it's not trying loading the final image into RAM (it uses very little memory during the process) but rather streaming it out to disk, which is ideal.

like image 737
meatvest Avatar asked Jan 21 '10 12:01

meatvest


2 Answers

You may try to use GDAL library. It provides bindings to Python. Here is combined tutorial presenting how to read and write images using C++, C and Python APIs Depending on GDAL operations and functions being used, GDAL can handle very large images and process images which are too large to be held in RAM.

like image 66
mloskot Avatar answered Nov 07 '22 08:11

mloskot


Not too suprising you're running out of memory; that image will take over 2gig in memory and depending on the system you're using your OS might not be able to allocate enough virtual memory to python to run it, regardless of your actual RAM.

You are definitely going to need to write it out incrementally. If you're using a raw format you could probably do this per row of images, if they are all of the same dimensions. Then you could concatenate the files, otherwise you'd have to be a bit more careful with how you encode the data.

like image 38
lepoetemaudit Avatar answered Nov 07 '22 09:11

lepoetemaudit