Smart resize fun with ImageMagick

TwitterDeliciousStumbleUponBlogger PostTechnorati FavoritesDiggLinkatopiaPingShare

So you just got back from vacation with a load of photos from your new super duper high res DSLR camera and now you wanna say, upload the photos to your blog, or maybe flickr or package up a nice zip and email it to grandma. Only one nagging problem. At multi-megabytes per image, even in an archive, this is still gonna take a long time.

You of course, love the high resolution for prints, but for your average screen resolution even, it’s really kind of overkill. If grandma opens the photo and sees a grainy washed out full color close up of a bird on a telephone pole that happened to be a hundred yards away at the time of your photo, she’s gonna be confused to say the least.

Enter, the wild world of the magical bash fairy and her friend ImageMagick. ImageMagick can do lots of different things for you, but it’s a little bit brutish to work with, so here’s a handy little script for your magickal image viewing pleasure. Oh yeah, don’t forget to always work on a copy of your pictures, I’m not responsible if they don’t come out the way you want! With that said, I think this script is pretty nifty.

Be sure to change the settings section to match your camera, but if you do, this script should automatically detect whether an image is landscape or portrait and it will also detect panoramic images and resize them down using a maximum value for the smaller dimension.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env bash
 
# I think this should work with sh as well as bash
 
# Settings {{{
#################################################################
    live_mode=true
 
    use_min_pan=true
    strip_exif=true
 
    # in pixels
    camera_width=4000
    camera_height=3000
 
    desired_min_pan=750
    desired_pan_width=1000
    desired_pan_height=750
 
    # in percent
    non_panoramic_ratio=25
 
# }}}
# Script {{{
#################################################################
    # get the width and height of the image {{{
    #################################################################
        img_width=$(identify -format "%w" $1)
        img_height=$(identify -format "%h" $1)
    # }}}
    # figure out which dimension is bigger {{{
    #################################################################
        max=$(if [ "$(( $img_height > $img_width ))" -eq 1 ]; then echo $img_height; else echo $img_width; fi)
        min=$(if [ "$(( $img_height < $img_width ))" -eq 1 ]; then echo $img_height; else echo $img_width; fi)
    # }}}
    # find the ratio {{{
    #################################################################
        ratio=$(echo "scale=2;($min/$max)*100" | bc | sed -e "s/\.[0-9][0-9]$//")
    # }}}
    # is it a panoramic image {{{
    #################################################################
 
        if [ "$(( $ratio < 75 ))" -eq 1 ]; then
            pan_status="Panoramic "
            is_panorama=true
        else
            pan_status=""
            is_panorama=false
        fi
    # }}}
    # determine orientation {{{
    #################################################################
        if [ "$(( $img_height > $img_width ))" -eq 1 ]; then
            orientation_is_portrait=true
            orientation_is_landscape=false
        else
            orientation_is_landscape=true
            orientation_is_portrait=false
        fi
    # }}}
    # determine resize ratio {{{
    #################################################################
        if $is_panorama; then
            if $use_min_pan; then
                # resize to a standard smaller dimension
                resize_ratio=$(echo "scale=5;$desired_min_pan/$min" | bc)
            else
                # resize depending on orientation to fit the screen better
                if [[ $orientation = "Portrait" ]]; then
                    resize_ratio=$(echo "scale=5;$desired_pan_width/$img_width" | bc)
                else
                    resize_ratio=$(echo "scale=5;$desired_pan_height/$img_height" | bc)
                fi
            fi
        else
            resize_ratio=$(echo "scale=2;$non_panoramic_ratio/100" | bc)
        fi
    # }}}
    # get resized dimensions {{{
    #################################################################
        new_width=$(echo "scale=5;$resize_ratio*$img_width" | bc | sed -e "s/\.[0-9]*$//")
        new_height=$(echo "scale=5;$resize_ratio*$img_height" | bc | sed -e "s/\.[0-9]*$//")
    # }}}
    # determine panoramic resizing {{{
    #################################################################
    if $is_panorama; then
        if $use_min_pan; then
            if $orientation_is_portrait; then
                resize_arg="${desired_min_pan}"
                dimensions="${desired_min_pan}x${new_height}"
            else
                resize_arg="x${desired_min_pan}"
                dimensions="${new_width}x${desired_min_pan}"
            fi
        else
            if $orientation_is_portrait; then
                resize_arg="${desired_pan_width}"
                dimensions="${desired_pan_width}x${new_height}"
            else
                resize_arg="x${desired_pan_height}"
                dimensions="${new_width}x${desired_pan_height}"
            fi
        fi
    fi
    # }}}
    # process the image {{{
    #################################################################
        # display status
        echo "$1 is a ${pan_status}${orientation} at ${img_width}x${img_height} with a ratio of 0.${ratio}"
 
        # resize the image
        if $is_panorama; then
            echo "Resizing to approximately ${dimensions} using \"${resize_arg}\"..."
 
            if $live_mode; then
                mogrify -resize ${resize_arg} "$1"
            fi
        else
            echo "Resizing to ${new_width}x${new_height} using \"${non_panoramic_ratio}%\"..."
 
            if $live_mode; then
                mogrify -resize ${non_panoramic_ratio}% "$1"
            fi
        fi
 
        if $strip_exif; then
            # strip the exif data
            echo "Stripping EXIF..."
 
            if $live_mode; then
                mogrify -strip "$1"
            fi
        fi
 
        echo ""
    # }}}
# }}}
TwitterDeliciousStumbleUponBlogger PostTechnorati FavoritesDiggLinkatopiaPingShare
This entry was posted in Notes and Quick Fixes and tagged , . Bookmark the permalink.

Comments are closed.

Rock Solid Web Design is a full service web development firm specializing in custom database driven applications in PHP.