0

I have hundreds of jpgs of varying sizes (e.g. 2304px x 2323px).

In gimp I can use a batch filter to change these to certain sizes, relative or absolute. But for some configurations I have to do the following manually, which for all the images takes forever:

  1. Change the size of the shortest side to 500px, maintaining the aspect ratio so the longer side is at least 500px. So if the image was 1000 x 1200, it will now be 500 x 600. The images come in both portrait and landscape.
  2. Change the canvas size so the image is a 500px x 500px square, centered. This will cut off part of the image (which is fine, most images are almost square anyway).
  3. Export the file with a -s appended to the file name.

Is there a script I can use to automate these steps?

Michael Millar
  • 1,364
  • 2
  • 24
  • 46

2 Answers2

0

Unless you find your way with gimp, you may want to look into ImageMagick: the mogrify tool allows to modify and resize images.

Beware: mogrify will overwrite your file, unless you use stdin/stdout. You probably want a script like this:

#!/bin/sh
for image in `ls /your/path/*jpg`; do
    mogrify -resize ... - < "$image" > "${image%%.png}-s.jpg"
done
Dacav
  • 13,590
  • 11
  • 60
  • 87
  • thanks, gimp can modify and resize too, I haven't used ImageMagick. Is there a way for ImageMagick that can automate those manual steps above? – Michael Millar Dec 11 '15 at 10:51
  • Unfortunately I'm not an expert imagemagick user. I just used it occasionally to convert images in a bach (using the `convert` utility). However, the `-resize` function is probably what you need for the resizing part. There is probably a way of centering the image as well. I suggest you to dig into the manual. Finally the renaming is just a matter of scripting. – Dacav Dec 11 '15 at 10:59
0

Something like this in ImageMagick. It's not as hard as it looks as most of it is comment. Try to on a COPY of your files - it does all the JPEGs in the current directory.

#!/bin/bash

shopt -s nullglob
shopt -s nocaseglob

for f in *.jpg; do
   echo Processing $f...

   # Get width and height
   read w h < <(convert "$f" -format "%w %h" info: )
   echo Width: $w, Height: $h

   # Determine new name, by stripping extension and adding "s"
   new=${f%.*}
   new="${new}-s.jpg"
   echo New name: $new

   # Determine lesser of width and height
   if [ $w -lt $h ]; then
      geom="500x"
   else
      geom="x500"
   fi
   convert "$f" -resize $geom -gravity center -crop 500x500+0+0! "$new"
done
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • looks good, will save me tons of time. Works well for jpgs as I put in my question. (Actually when I convert .tif files it's producing weird colours (they're in CMYK) but that's probably for a separate question. I can use `-colorspace CMYK` in the last convert line to keep them in CYMK but it's not quite right). Thanks! – Michael Millar Dec 11 '15 at 13:13