1

I have a folder of colourful images in PNG and JPEG. By using file in linux, I can see PNG images are all PNG image data, ... x ..., 8-bit/color RGBA, non-interlaced where their height and width are different. And JPEG images are all JPEG image data, JFIF standard 1.01. Their size can be up to 500KB.

I can use find . -type f -size +10k -exec ls -l {} \; to list all the images whose size is above 10Kb.

I can also use convert input.png -geometry x150 output.png to resize one image's height to 150px without changing the height/width ratio. or mogrify -geometry x150 * to resize all the images in the folder.

Does anyone know how combine these 2 commands and resize all the images whose size is above 10Kb in the folder?

  • Do you want to keep the original pictures and makes small copies? – sudodus Apr 17 '18 at 6:16
  • It is not necessary to keep the original pictures... – SoftTimur Apr 17 '18 at 6:18
1

I assume that there are only picture files in the current directory. If not, you may specify two commands, one with -name "*.jpg" and one with -name "*.png".

Try this command to see which files are selected (avoid destroying files you want to keep),

find . -type f -name "*" -size +10k -exec echo mogrify -geometry x150 {} \;

and if OK, remove echo to use this command to shrink the files that are too big

find . -type f -name "*" -size +10k -exec mogrify -geometry x150 {} \;

Maybe the 10k size does not match -geometry x150, so you should modify it depending on the kind of pictures.

  • I think you can omit -name "*". – dessert Apr 17 '18 at 15:14
  • 1
    @dessert, Yes you are right, but I put it there as a 'placeholder' to show where to insert -name "*.jpg" or -name "*.png" if necessary to skip other files in the directory tree. – sudodus Apr 17 '18 at 15:20
2

Why not execute the convert from find?

find . -type f -size +10k -exec convert {} -geometry x150 {}.png \;

You need to rename the files from *.pic.png to something useful. But it should work.

  • Shouldn't this use mogrify to in-place change the file, or specify an output file? – muru Apr 17 '18 at 6:43
  • @muru sure, but the in-place way could lead to permanent loss the files when testing/debugging the script – Simon Sudler Apr 17 '18 at 6:47
1

This will create a folder with your resized images and not touch your original files, and place the resized images there.

This will reserve your original images of which you can replace if you are satisfied with the new resized images.

#!/bin/bash

[ ! -d resized ] && mkdir resized

while read image; do
    echo "Resizing [$image] to [resized/$image]"
    convert $image -geometry x150 "resized/$image"
done < <(find . -type f -size +10k -exec ls {} \;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.