1

Let's say I have a folder with many subfolders in it. Each of these subfolders contain many images.

I want to resize these images in each subfolders with the following conditions:

  • If the image's height is less than 1000 pixels, then do noting.
  • If the image's hieght is between 1001 to 2500 pixels, then resize it down to 70%.
  • If the image's hieght is between 2501 to 3000 pixels, then resize it down to 50%.
  • If the image's hieght is more than 3000 pixels, then resize it down to 30%.
  • the new images replaces the old ones.

Normally I can do this with a few images, by looking at image's information one-by-one and using mogrify command, but this time I have too many subfolders. Since I'm a new linux user, any help would be appreciated.

1

I modified the code that jajo provided and got it working this way:

MIN_RES=1001
MAX_RES=2500
RES11=2501
RES12=3000
RES21=3001
RES22=10000

    for i in *.jpg; do
YRES=$(identify -verbose $i|grep Geometry: |cut -d 'x' -f2 |sed 's|+.*||g')
if [ $YRES -gt $MIN_RES ] && [ $YRES -lt $MAX_RES ]; then

echo "Found: $i with y-res: $YRES";

# convert
convert -resize 70% $i $i;

elif [ $YRES -gt $RES11 ] && [ $YRES -lt $RES12 ]; then

echo "Found: $i with y-res: $YRES";

convert -resize 50% $i $i;

else [ $YRES -gt $RES21 ] && [ $YRES -lt $RES22 ]

echo "Found: $i with y-res: $YRES";

convert -resize 30% $i $i;

fi
done
3
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – peterh
    Mar 13 '18 at 17:48
  • @peterh, note that they claim to be the OP, so merging their accounts would be a more appropriate course of action.
    – Jeff Schaller
    Mar 13 '18 at 19:15
  • 1
    peterh's advice still holds; if you have an answer for the current question, accept it and then ask a separate question for any major differences.
    – Jeff Schaller
    Mar 13 '18 at 19:16
0

You'll find here : https://askubuntu.com/questions/1164/how-to-easily-resize-images-via-command-line some details on command-line tools offered by ImageMagick.

So, to perform the job on a single picture, I would :

  • use identify tool to read metadata of pictures, for example:

    $ identify /tmp/p1.jpg /tmp/p1.jpg JPEG 1487x925 1487x925+0+0 8-bit sRGB 314106B 0.000u 0:00.000

  • interpret output tou extract dimensions 1487x925

  • apply my rules to build a convert command.

A loop based on " find " command results would allow to repeat this for one or more folders.

0

you can use from ImageMagic toolbox "identify" for reading image resolution and "convert" for converting images. Something like this:

MIN_RES=100
MAX_RES=500
for i in *.jpg; do
    YRES=$(identify -verbose $i|grep Geometry: |cut -d 'x' -f2 |sed 's|+.*||g')
    if [ $YRES -gt $MIN_RES ] && [ $YRES -lt $MAX_RES ]; then

        echo "Found: $i with y-res: $YRES";

        # convert
        convert -resize 50% $i $i;

    fi
done

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.