Every so often, I try to upload a picture to a website and encounter a message:

Upload failed: the file size can't exceed n MB!

I know that resizing the picture's dimension should get me below n at a certain point, but the process of resizing, saving, checking file size is cumbersome. Furthermore, at times it may be handy to keep the current dimensions.

I'm looking for a way to change a picture's file size, preferably from the terminal. Is there a way to do this?

  • You could try jpegoptim which is a commandline too to optimize jpegs. In general i would think about either resizing or compressing or a combination of both if your target is reducing filesize
    – dufte
    Aug 30, 2016 at 9:56
  • You could use another type of compression, .jpg instead of .png. ImageMagick (imagemagick.org) has both commandline an basic graphical interface
    – Pit
    Aug 30, 2016 at 9:59
  • ImageMagick has a switch that allows to set the file size (imagemagick.org/Usage/formats/#jpg_write), unfortunately only for jpg (similar to jpegoptim). Quote: 'It works by generating many versions of the JPEG image, doing a binary search, of the output quality "-quality" setting, until it gets as close as possible to the file size given without exceeding it' (end quote). Maybe you can write a script that mimicks this behaviour for other formats.
    – Marijn
    Aug 30, 2016 at 14:34

1 Answer 1



First install jpegoptim via

sudo apt install jpegoptim

Then either try to optimize filesize of your image via

jpegoptim /path/to/image.jpg

or if you need a defined size via

jpegoptim --size=512k /path/to/image.jpg

This will overwrite the image - to make sure you have a backup while testing with jpegoptim.

mogrify (imagemagick)


You could as well use mogrify which is part of imagemagick. The following example shrinks the image dimensions to 50% (so resizing - not compressing)

mogrify -resize 50% /path/to/image.jpg


The following example shows compressing with mogrify:

mogrify -compress JPEG -quality 50 /path/to/image.jpg

convert (imagemagick)


Compressing to 80% using convert works as follows:

convert -quality 80% /path/to/source/image.jpg /path/to/result/image.jpg

Convert somes with a huge set of options, so you could use

convert -strip -interlace Plane -gaussian-blur 0.05 -quality 85% /path/to/source/image.jpg /path/to/result/image.jpg

which should according to this post:

  • set the quality to 85
  • progressive (comprobed compression)
  • a very tiny gausssian blur to optimize the size (0.05 or 0.5 of radius) depends on the quality and size of the picture, this notably optimizes the size of the jpeg.
  • Strip any comment or exif tag


I am quoting the project page here

Trimage is a cross-platform GUI and command-line interface to optimize image files for websites, using optipng, pngcrush, advpng and jpegoptim, depending on the filetype (currently, PNG and JPG files are supported).

You can simply install it via

sudo apt install trimage

After starting via just the application name you might see the UI

enter image description here

If you need more information about trimage either visit the project page or run trimage -h to take a look on the help.

Other tools

There is as well optipng, pngcrush, jpegtran and several other tools in the image-size-optimization-corner .... but i've never used them.

I'll end with some links

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .