Shilpi Agrawal
Shilpi Agrawal

Reputation: 173

compress very large resolution image file using Imagemagick

I have some scanned images of resolution 20400x28056. I want them to be compressed using imagemagick. I tried convert ToBeCompressed.jpeg -quality 70 output.jpeg but this command is making my laptop crashed. I think it is not able to handle such large resolution. Any suggestions please.

Upvotes: 3

Views: 3303

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207465

A few things to check... your image corresponds to around 1.6GB of raw data so you may find you need to be running a 64-bit environment, rather than 32-bit. If you are on Windows, you may need to be running on an NTFS filesystem rather than FAT because FAT filesystems, though they can be up to 130GB in size, cannot support individual files larger than 2GB, or maybe 4GB if you are lucky.

Finally, could you consider resizing the image rather than reducing its quality, if you resize it on-the-fly as you read it in, you may do better since it is less resource-intensive. So, say you decided you could accept an image half the length on each side (1/4 of the original area)

convert input.jpg[50%x50%] -quality 70 output.jpg

Depending on what you actually want to do with the image afterwards, you could use ImageMagick's stream utility to split the image into chunks. It reads the image a row at a time so it only uses very little memory. So, to get the two halves of the image into a raw format, you could do

stream  -extract 20400x14028+0+0     ToBeCompressed.jpg tophalf.rgb
stream  -extract 20400x14028+0+14028 ToBeCompressed.jpg bottomhalf.rgb

Then, if you want to convert those raw RGB files to JPG, you can do

convert -depth 8 -size 20400x14028 tophalf.rgb tophalf.jpg

Another avenue you could pursue might be vips, which is described here.

So, if your image is called BigBoy.jpg you could do this to set the quality to 60, for example:

vips jpegsave BigBoy.jpg BB60.jpg --Q=60

which results in this on my system

ls -l Bi* BB60.jpg
-rw-r--r--  1 mark  staff  270283175 19 Mar 14:17 BB60.jpg
-rw-r--r--  1 mark  staff  997198223 19 Mar 14:07 BigBoy.jpg

Upvotes: 7

Related Questions