Generate hundreds of resolutions from a single image
I want to make lots of images based on a source image!
Obviously I could do this by hand in Adobe Photoshop... and this would take a silly amount of manual time.
You are so right. Let the computer do the easy repetitive work; that's what they are good at. As Tom Ruh's answer notes, you can use ImageMagick
for this.
However, there is a problem:
Lucky enough the image is 4:3 resolution and would work well with all resolutions with the same height and width, i.e 80x80px 100x100px etc.
(emphasis mine)
It's not clear what resolution you want- 4:3 as stated, or 1:1 as implied.
However, based on your requirements of minimum 73px and maximum 1000px and hundereds of resolutions; I've written up a little script which should cover most possibilities (including liquid resizing on the off-chance you'd like that) which can be set by changing some variables.
Script, also available from pastebin here for easier copying:
#!/bin/bash
# resizer.sh - resize target image between two resolutions
# accepts file as either first argument or by setting FILEPATH variable
# SETTINGS
SMALLEST_WIDTH=73 # px
LARGEST_WIDTH=1000 # px
FILEPATH= # set if you don't want to pass in image as argument
NUM_OF_RESOLUTIONS=100 # number of images generated; will generate between
# $SMALLEST_WIDTH and $LARGEST_WIDTH
RATIO= # set if you want to specify width/height
# (eg 1/1, 4/3, 16/9), blank is preserve current ratio
# NOTE: resizing to other aspect ratios may be slow/distorty:
# as per http://www.imagemagick.org/Usage/resize/#noaspect
# Seamless resizing (default) may be preferred, see:
# http://www.imagemagick.org/Usage/resize/#liquid-rescale
# but note it is slower, particularly as images get larger
LIQUID=0
# SCRIPT BELOW
# silent by default; uncomment "printf" lines for a description of what is happening
die() { printf "$@\n" 1>&2 ; exit 1; }
if [ -z "$FILEPATH" ]; then
if [ -z "$1" ]; then die "Need to supply file to work on either as argument or by setting FILEPATH!";
else FILE="$1";
fi
else
FILE="$FILEPATH"
fi
# check file exists and is regular file
if [ ! -e "$FILE" ]; then die "$FILE does not exist!"; fi
if [ ! -f "$FILE" ]; then die "$FILE is not a regular file!"; fi
i=0
step=$(echo "($LARGEST_WIDTH - $SMALLEST_WIDTH) / ($NUM_OF_RESOLUTIONS - 1)" | bc -l)
#printf "Resolution step is: %s\n-------------" "$step"
while [ $i -lt $NUM_OF_RESOLUTIONS ]; do
# handle ratio
WIDTH=$(echo "$SMALLEST_WIDTH+($step*$i)" | bc -l)
if [ -z "$RATIO" ]; then
#printf "convert %s -resize %s %s\n" "$FILE" "$WIDTH" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
convert "$FILE" -resize "$WIDTH" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
else
HEIGHT=$(echo "$WIDTH * $RATIO" | bc -l)
if [ "$LIQUID" -eq 0 ]; then
# Uncomment convert line for distorted ("squashed") resizing
#printf "convert %s -resize %sx%s\! %s\n" "$FILE" "$WIDTH" "$HEIGHT" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
convert "$FILE" -resize "$WIDTH"x"$HEIGHT"\! "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
else
# Liquid resizing: http://www.imagemagick.org/Usage/resize/#liquid-rescale
# fast aspect ration resize first, then liquid
#printf "convert %s -resize %s %s\n" "$FILE" "$WIDTH" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
convert "$FILE" -resize "$WIDTH" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
#printf "%s details are now:\n %s\n" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}" "$(identify "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}")"
#printf "convert %s -liquid-rescale %sx%s\! %s\n" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}" "$WIDTH" "$HEIGHT" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
convert "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}" -liquid-rescale "$WIDTH"x"$HEIGHT"\! "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
fi
fi
(( i++ ))
done
Notes: A bit over-the-top on subshells for calculating values and such, but hey ho. As noted, the printf
lines can be uncommented for an idea of what is going on, otherwise it will operate silently by default as per the Rule of Silence. Some images wont be dimensions exactly as calculated (eg 193px vs 138.54545454545454545452px) because you can't in a useful way have fractional pixels.
As long as you can handle some command line and write some simple BASH scripting. You can use ImageMagick