1

I have a file with the full path to an image on each line, all are jpg. I want to resize every image/line in the file to 1024px in width, while keeping the aspect ratio for the height. I simply want to overwrite the original file.

The main thing I'm having issue with is, I want to SHRINK only. I do not want to enlarge images smaller than 1024px in width up to 1024px in width.

Here is the bash script I'm working with and just need help with the convert line as I have no experience with imagemagick.

#!/bin/bash

while read p; do
  convert $p
done < listofimages
Sajan Parikh
  • 1,095

2 Answers2

2

This should do it:

#!/bin/bash

while IFS= read -r jpg
do
    convert "$jpg""[1024x>]" "$jpg"
done < "$1"

Save the script above as ~/bin/shrink.sh, make it executable (chmod a+x ~/bin/shrink.sh) and run it, giving the list of files as an argument:

shrink.sh /path/to/list

This is basically the same as the proposed duplicate, adapted to i) match your desired size and ii) read the names from a file. The [1024x>] ensures that only images whose size is greater than 1024 pixels will be resized.

terdon
  • 104,119
-1

Try something like this:

#!/bin/bash

for p in "$@"
do
    w=`identify "$p" | cut -f 3 -d ' ' | sed 's/x.*//'`
    if [ $w -gt 1024 ]
    then
        convert -scale 1024x1024 "$p" "$p"
    fi
done

Explanation:

  • The "identify" command produces all sorts of info about the image. The third field is the size, in the format "1024x968".
  • The "cut" command grabs the third field, delimited by spaces.
  • The "sed" command deletes the height, leaving the width.
  • The "if" only processes images whose width is larger than 1024.
  • Finally, convert rescales the image. It automatically keeps the aspect ratio.

If you've got file names in a file, and name the above script "reaspect", you can use this:

% reaspect `cat file`