Take the 2-minute tour ×

I have a number of JPEG pictures which I would like to scale down. Another requirement is that the file size should not be larger than say 300kByte.

Is that possible, please help me with an example =)

share|improve this question

2 Answers 2

up vote 15 down vote accepted

To restrict the resulting image to a maximum file size, you can use the commandline parameter -define jpeg:extent=max_value, like this:

convert original.jpeg -define jpeg:extent=300kb output.jpg
convert original.jpeg -define jpeg:extent=300kb -scale 50% output.jpg
convert original.jpeg -define jpeg:extent=300kb [...other options...] output.jpg

Note, this will not always work to match the exact size you wanted. You may have asked for 40kb output size, where input is 300kb, and get a result of 48kb.

(Update/Clarification: Output file size may be a bit lower or higher than your file requested size.)

share|improve this answer
I think it is the other way around. You may get a smaller file than you ask for but not bigger - imagemagick.org/script/command-line-options.php#define –  Mark Setchell Aug 12 at 14:53
@MarkSetchell: My statement about output file size was maybe a bit un-precise. I did not mean to imply it's always larger than requested. It did want to show it's not exactly as requested. Also, ImageMagick sometimes changes behaviour with new releases, and I did not specifically test if this feature changed recently. –  Kurt Pfeifle Aug 12 at 18:48
@MarkSetchell for x in *.JPG; do mogrify -define jpeg:extent=8192 $x; done gets me a dozen files of around 12 kB, so bigger is also possible. –  mirabilos Aug 18 at 9:17

The jpegoptim tool (actual homepage is for multiple programs) works better for me:

jpegoptim -s -S8 *.JPG
  • -s means to strip all metadata (EXIF, JFIF, XMP, etc.)
  • -S8 means to target a filesize of about 8 KiB

You can crunch them even better by running it twice, because empirically, most images are smaller as progressive:

jpegoptim -s --all-progressive -S8 *.JPG; jpegoptim -s --all-normal -S8 *.JPG

jpegoptim will normally refuse to write an image that increases the size, so this will give you optimum quality/size.

share|improve this answer
I’ve edited the answer to add the idea to run both --all-progressive and --all-normal. Hope that helps. –  mirabilos Dec 23 at 13:48

Your Answer


