akash
akash

Reputation: 1801

Downscaling image file size using imageMagick

I am trying to find out an imagemagick operation to reduce image filesize by decreasing its quality.

This operation works

convert -fuzz 1% -trim -quality 90 -limit memory 32MiB  original.jpg  converted.jpg

Reducing the quality factor will decrease the imageSize. Is there any other way of acheaving the same and adding a limit of MAX_SIZE.

For example 5Mb image should be downsized to 2Mb

Upvotes: 0

Views: 967

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 208052

You would use -define jpeg:extent=....

Here is an example with a large image of random data that would need a very large file size to accurately represent it with any reasonable quality.

convert -size 10000x1000 xc:gray +noise random -define jpeg:extent=2MB out.jpg

Result

-rw-r--r--    1 mark  staff  1844050 15 May 10:44 out.jpg

And check the quality used:

identify -format "%Q" out.jpg 
21

Another example:

convert -size 10000x1000 xc:gray +noise random -define jpeg:extent=400kb out.jpg

Result

-rw-r--r--    1 mark  staff   377757 15 May 10:44 out.jpg

And check the quality used:

identify -format "%Q" out.jpg 
5

If you want a way to do something similar with Python, I wrote an answer that works pretty well here. It does a binary search for a JPEG quality that satisfies a maximum size requirement.

Upvotes: 2

xenoid
xenoid

Reputation: 8994

Besides quality, an important factor is chroma-subsampling. By default IM uses a "halved chroma", but you can use "quartered chroma". See this question for details.

Check the jpeg:extent option in IM's convert:

You can also use Google's Guetzli. As far as I understand it, it tries various JPEG encoding options to reduce file size, while checking the visual acceptability of the result.

Upvotes: 0

Related Questions