8

I'm working on a project where I need a single image in multiple resolutions. Lucky enough the image is identical width/height resolution and would work well with all resolutions with the same height and width, i.e 80x80px 100x100px etc.

Obviously I could do this by hand in Adobe Photoshop but I literally need hundreds of different resolutions from 73px all the way up to 1000px and this would take a silly amount of manual time. I've searched the net and all I keep getting is thumbnail generators or Adobe Photoshop batch saver which would still take a considerable amount of time.

So, which brings me to my question, How can I create hundreds of resolutions from a single image as quickly as possible, either in Windows or Linux.

9
  • 1) they have CDNs like ImgIx that will resize images dynamically for you. 2) I've used ThumbNailer by Smaller Animals ($15) to batch process images. You just run it via command line with "profiles" you configure and call through command line. For each resolution, you would need a separate profile. Since your images are 1:1 and you don't need any "intelligent" resizing options, I think ImageMagick will be more than sufficient.
    – Sun
    Jul 13, 2015 at 22:03
  • Could you use a single vector image instead?
    – Anonsage
    Jul 14, 2015 at 1:18
  • 2
    I'm curious as to the project that would require hundreds of the same image at different sizes. It's not relevant to the answer, but I'm curious nonetheless.
    – user351775
    Jul 14, 2015 at 5:33
  • 2
    Are you SURE you need an image in so many resolutions? Think about it again, much more carefully. Discuss it with other people. I can't rule out there's an edge case somewhere where this is really the case, but if I had to bet, I'd bet on "you are doing it wrong".
    – o0'.
    Jul 14, 2015 at 8:06
  • @Lohrois your right we don’t need them all, but for the sake of 30mb in total (1000pngs) it’s not a big issue for PNGs not used. Jul 14, 2015 at 8:23

2 Answers 2

19

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.

10
  • 1
    Woohoo, awesome! just tested and it worked lovely, sadly however this creates a new issue of file labels, as 1, 2, 3, 4 etc doesn't inform me the resolution without having to do some math or by inspecting the image. Do you happen to know how I can adapt the script to append resolution to filename i.e filename-500x500.png rather than filename-1.png Jul 13, 2015 at 20:40
  • Glad it works to your more-or-less satisfaction. Thanks for clarifying, I will update the answer with a more descriptive filename. Would filename-width.png suffice? One of the modes (keep original aspect ratio) doesn't directly calculate an explicit height. I can easily enough get the information (via identify) but it would save an additional per-image call in cases of original aspect ratio.
    – bertieb
    Jul 13, 2015 at 20:43
  • Woah, thanks for the quick response! yep the width would do the job since I'd auto assume the same number for the height. Jul 13, 2015 at 20:44
  • In regards of what I meant by 4:3 was aspect ratio found in TVs and Monitors, Square ones.. i.e google.co.uk/… and not letter box 16:9 etc. Jul 13, 2015 at 20:45
  • 1
    I'll have to download it, thanks! listen!.... your a legend, the script works awesomely! thank you for all your help. Jul 13, 2015 at 21:02
7

As long as you can handle some command line and write some simple BASH scripting. You can use ImageMagick

3
  • 1
    If the resolution is in 4:3 aspect ration, then for ever 4 pixels wide, you go another 3 up. In addition, 80x80 or 100x100 resolutions have a 1:1 aspect ratio. Since you going from a 4:3 ratio to a 1:1 ratio, you will either have to stretch the image or crop excess pixels. Not sure if it matters but couldn't you just keep your 4:3 resolution to avoid this?
    – Geruta
    Jul 13, 2015 at 13:07
  • 1
    It's comforting to know that a solution is possible, but this doesn't answer how to accomplish it. Can you expand your answer with an example or some instructions? Thanks.
    – fixer1234
    Jul 13, 2015 at 21:20
  • 1) You can use convert.exe which is part of ImageMagic with the -resize option: imagemagick.org/discourse-server/viewtopic.php?t=15438
    – Sun
    Jul 13, 2015 at 22:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .