3

I know there are several ways to resize an image via the command line. Some of them are specified here. However, I was wondering whether there is a way to specify the size of the resized image, e.g. I want to resize an image that is 8.5 MB big to an image that is 2.5 MB. Preferably, the solution uses imagemagick.

hyhno01
  • 45
  • Do you want to achieve the reduction solely through image data compression, or are you ready to change image resolution (pixel count) too? – Levente Jun 15 '21 at 12:30
  • I prefer data compression. The goal is to achieve the best image quality, but reduce the size so that I can upload the image to an online-forum – hyhno01 Jun 15 '21 at 12:36
  • Resize the image to the resolution of a screen (i.e., HD is 1920x1080) and you will already have an image far less than 8 MB and in max quality for screen display. – vanadium Jun 15 '21 at 12:59
  • Thanks for the recommendation. Though it does not solve my problem. The ratios of the images may differ (2:3, 3:4, 16:19 etc.) and I want to make sure that every image ends up being 2.5 MB which is the maximal size for upload in the forum and thus ensures the best quality – hyhno01 Jun 15 '21 at 13:03
  • Yes it does. And no, 2,5 MB is not necessarily ensuring the best quality. For on screen display, it probably will be overkill. Start with reducing the size to the display resolution. That will usually bring you already below 2 MB. If that doesn't, increase the jpeg compression until it does. The result will be very acceptable. If it is only a few images, Gimp allows to see the resulting file size while you tweak the compression settings. – vanadium Jun 15 '21 at 13:07
  • I found an obscure setting allowing to set the maximum size: see my answer. Still, for your application, prefer to adjust the image to the screen display resolution first. You can go much lower than 2 Mb for optimum on screen display. – vanadium Jun 15 '21 at 14:47

1 Answers1

5

With credit to here, there is a jpeg codec option for Imagemagick's convert supports an option for jpeg that allows to restrict the size of the output file: -define jpeg:extent=max_value. It is used as:

convert test.jpg -define jpeg:extent=1M testout.jpg

If anyone could tell where such settings are officially documented, I will include it in this answer.

vanadium
  • 88,010