Linux – crop an image without fully loading it into memory

decodingimage editinglinuxmemorypng

I managed to generate a PNG image with a fractal.

The image is 65,536 pixels high and 65,536 pixels wide. It’s too big to be decoded into memory and displayed. It has probably a lot of unused space near the borders and I want to trim it to significantly reduce its area.

I tried GIMP and GraphicsMagick, but GIMP froze my computer and GraphicsMagick failed to allocate enough memory for the image.

I use Linux and I have 16GB of RAM. The compressed image is 6.2MB in size.

Can I trim the borders without loading the image fully into memory?

FWIW, in the end I used my school's server but I still want to know the answer.

Best Answer

You already know the cartesian definition of the image.

Consider some Python code to read the file in reasonably-sized buffer-loads - choose one or multiple lines of image pixels per buffer, but sufficiently few with regard to memory-availability.

On most pixel-lines, the left-most and right-most "border" pixels can be discarded. The first few, and last few, "border" lines, similarly.

The buffer-size of the output file can, and will, be different to that of the input file, by definition.

Related Question