0

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

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
akash
  • 1,653
  • 5
  • 22
  • 38
  • Out of interest you should sort out your command order. In most cases it is "convert input options output". You can usually get away with an incorrect order in V6 but V7 is stricter. This could save you some confusion if/when you move up to V7. – Bonzo May 15 '17 at 11:17
  • @Bonzo Thanks , I will keep that in mind – akash May 17 '17 at 06:56

2 Answers2

2

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.

Mark Setchell
  • 146,975
  • 21
  • 182
  • 306
0

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.

Community
  • 1
  • 1
xenoid
  • 5,908
  • 2
  • 18
  • 37
  • Please note that proper IM 6 syntax read a raster image before applying settings then operators. So read you input right after the convert command. – fmw42 May 15 '17 at 18:20