Imagemagick converting horizontal images

Jeffrey F. Bloss jbloss at tampabay.rr.com
Thu Feb 22 16:26:17 UTC 2007


Marcanth wrote:

> 
> Hi,
> 
> I have about 30 images and would like to resize only the horizontal
> ones. How could I do this with ImageMagick -resize option? Example:

The "horizontal" ones are what's called "landscape orientated". The
tall thin ones are "portrait". Just for future reference. ;)

> 
> image01.jpg (width: 400, height: 200). This image (horizontal) will be
> resized to, let's say, 200x100.
> 
> image02.jpg (width: 100, height: 500). This one is vertical, so it
> will not be affected.
> 
> How could I do this?

Imagemagick includes a utility called 'identify' which outputs a single
line of info by default, which includes geometry. I'd leverage that in
a bash script. Something like this oversimplified, untested, and
pseudo-code infested example. ;)


### BEGIN SCRIPT
#! /bin/bash

for i in $(ls)         ## What, no wildcards!?! 
do
    #############################################
    ## This should be done with sed + regex, 
    ## but 'cut' is just easier to understand. 
    ## If there's spaces in file names it breaks!
    #############################################
    GEOMETRY=$(identify $i | cut -d' ' -f 3)
    IWIDTH=$(echo $GEOMETRY | cut -d'x' -f 1)
    IHEIGHT=$(echo $GEOMETRY | cut -d'x' -f 2)

    ##############################################
    ### Now that we have our IWIDTH and IHEIGHT we
    ### test and resize if it's landscape oriented
    ##############################################
    if [ $IWIDTH -gt $IHEIGHT ] ; then
        echo "Processing image $i ..."
        convert -size <new-geometry> $i -write <outfile>
    fi
done
### END SCRIPT

-- 
     _?_      Outside of a dog, a book is a man's best friend.
    (o o)         Inside of a dog, it's too dark to read.
-oOO-(_)--OOo------------------------------[ Groucho Marx ]---
                    http://wrench.homelinux.net/~jeff/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 892 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20070222/018ecc46/attachment.sig>


More information about the ubuntu-users mailing list