Imagemagick Convert

Resizing Images can be a tedious task. Imagemagick comes to the rescue.

Resize According to Image Orientation

#!/bin/bash
 
if [ -z $1 ]; then
  echo "Prefix?"
  exit 1
fi
 
prefix=$1
 
# case insensitive globbing:
shopt -s nocaseglob
files=(*.jpg)
 
# numeric id for filenames
id=0
 
for f in "${files[@]}"; do
  # increment id
  let id++
  # format id with preceding 0 if we're below 10
  if [ $id -lt 10 ]; then
     suffix=$(printf '%02d' $id)
  else
     suffix=$id
  fi
  echo "$f --> ${prefix}_${suffix}.jpg"
  convert -auto-orient -resize '800x600>' -strip $f $prefix_$suffix.jpg
done

The real magic(k) comes with -resize '800×600>': this copes with landscape and portrait oriented images.

What it does: If the width OR height is greater than specified, resize to the given value.

So, a landscape image will be resized to 800px and a portrait image will be resized to 600px.

Resize with Regex Magic

This script generates thumbnails and the smaller-than-the-original images for a gallery.

It uses Regular Expressions to output the correct filenames according to the input filenames:

01.jpg -> preview_01.jpg
01.jpg -> slide_01.jpg
02.jpg -> preview_02.jpg
02.jpg -> slide_02.jpg
...
#!/bin/bash
 
files=(*.jpg)
 
for f in "${files[@]}"; do
  if [[ $f =~ (^[[:digit:]]{2})\.jpg$ ]]; then
    echo ${BASH_REMATCH[1]}
    convert -resize 96x64 -strip $f preview_${BASH_REMATCH[1]}.jpg
    convert -resize 800 -strip $f slide_${BASH_REMATCH[1]}.jpg
  fi
done
imagemagick/convert.txt · Last modified: 2016-03 by tb
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 ipv6 ready