2

I searched everywhere but couldn't find an answer to this. I would like to output all images of a folder in 50Kb exactly and maintain the original aspect ratio.

I tried ImageMagick and resizing to 250x250 e.g but it is not working for me, what it does is change the first dimension and adapt the other, so output images have not the same size.

for image in `ls $@*.jpg`; do
  mogrify "${image}" -resize 250x250 "${image}"
done

How do it? Thanks

Takichiii
  • 453
  • 4
  • 14
  • 27

1 Answers1

2

Updated Answer

If you want the file size of the images to be limited to 50kB, use:

convert input.jpg -define jpeg:extent=50KB output.jpg

Original Answer

Use mogrify, but be careful it will overwrite all your images:

mogrify -resize 250x250! *.jpg

In general, when using mogrify I like to use the -path option to specify an output directory for results, like this to avoid originals getting overwritten:

mkdir results
mogrify -path results ...

Your script will work if you add the ! after the resize which forces the resize even if it distorts the shape of the picture.


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
  • 191,897
  • 31
  • 273
  • 432
  • Thank you. I tried it and the size of output images varies between 41Kb to 55Kb. One picture which already was 41Kb got smaller (19,3Kb). I actually realized that my original code do the same exact thing that this command. The output images are not 50Kb exactly. Is there a way to fix this? – Takichiii Jan 06 '17 at 09:46
  • No, the defined extent is just a hint and ImageMagick does its best to match it as well as possible but the process is complicated as there is no way to predict accurately how well an image will compress. – Mark Setchell Jan 06 '17 at 09:56
  • I have a folder full of pictures of exactly 50,X.KB(not shrinked or stretched, dimensions are not constants) to test my code, I'm sure there is necessarily a simple way we can do this – Takichiii Jan 06 '17 at 10:05
  • Can I ask *why* you are fixated on 50kB - I have never heard of such a requirement. I can only suggest an iterative procedure whereby you slightly vary the number of colours or the sharpness or the pixel dimensions until you get sufficiently close to your target. – Mark Setchell Jan 06 '17 at 10:24
  • I am not fixed on 50Kb, i would just like to reproduce the same thing they did as it is possible and because it would be more efficient for me to fix their size (to have the same amout of informations to work with) and focus on other variations between them (keypoints differences). I hope I was clear enough – Takichiii Jan 06 '17 at 10:35
  • I am having a hard time keeping up with you... first your code showed you using `convert` but you are actually using `mogrify`, second your code implied you wanted to change the dimensions but you actually wanted to changed the file size (in bytes) and now you are talking about some *"they"* without saying who you mean or what *'they"* did. Please try to make your question actually reflect what you are trying to do. – Mark Setchell Jan 06 '17 at 10:48
  • Sorry I tought you understood my point. I changed my original answer just so that people don't have to read all the comments. I didn't use convert at first I told you it was just a mistake and I removed the comment after that because it was useless. My code does obviously not what i want and that's why I'm asking here. you wanted to know more on why i want to do this because "you have never heard of such a requirement", i told it is apparently possible because test images for a project were all in a precize size, and I want to know how i can do it too. – Takichiii Jan 06 '17 at 11:32
  • Please do not ask why i want to do it and why it is a requirement and complain after because im not too clear. I think my original question is very clear and precise tough and that's all people need to know. – Takichiii Jan 06 '17 at 11:33
  • If you need image files that are **exactly** 50k, you can iterate on the "-quality" value, decreasing it until your file is smaller than 50k, then add comments with the number of bytes you need for padding. I think ImageMagick actually provides a feature for doing the iterations automatically for you. – Glenn Randers-Pehrson Jan 07 '17 at 04:35