[mdlug] Question - bulk aspect modification of image files

Ingles, Raymond Raymond.Ingles at compuware.com
Mon Jun 1 13:02:40 EDT 2009


> From: Aaron Kulkis

> >  So, I'm hoping to use Imagemagick or something similar to take the
> > existing pictures and frame them, set them on a colored background
of
> > the appropriate size. I'm sure there's a way to do this, and I can
> > probably puzzle it out from the man page eventually, but I was
wondering
> > if someone more familiar with the tools has done something like this
> > already?

> I think that for your purposes, the results you want will be
> for each picture, create a degenerate case montage of ONE
> picture, with a border and/or frame.

 Close; below is the script I whipped up over lunch to do this. It ain't
pretty - I haven't cleaned up all the debug cruft or optimized it - but
it gets the job done.

---------------------
#!/bin/bash

#usage: aspect.sh <dir name> <images to convert...>

# Function to copy and, if needed, border the images to the target dir
# Forces a 4:3 aspect ratio
grab_image()
{
  CURIMG=$1
  LDIR=$2
  TNUM=$3
  TNAMENUM=`printf %03d ${TNUM}`
  echo Image number ${TNAMENUM}

  SIZE=`identify ${CURIMG} | cut -d " " -f 3`
  WIDTH=`echo ${SIZE} | cut -d "x" -f 1`
  HEIGHT=`echo $SIZE | cut -d "x" -f 2`
  TASP=`echo "scale=2; ${WIDTH} / ${HEIGHT} * 100" | bc`
  ASPECT=`echo ${TASP}| cut -d"." -f 1`
  echo $CURIMG $SIZE $WIDTH $HEIGHT ${ASPECT}

  if [ 133 -ne ${ASPECT} ]; then
    echo "Found one, aspect is ${ASPECT} !!!!"
    # Okay, we have to generate a border
    if [ ${ASPECT} -gt 133 ]; then
      echo "top & bottom"
      NEWHEIGHT=$(( ${WIDTH} * 3 / 4 ))
      BORDTOT=$(( ${NEWHEIGHT} - ${HEIGHT} ))
      BORDER=$(( ${BORDTOT} / 2 ))
      echo ${BORDER}
      convert ${CURIMG} -bordercolor Black -border 0x${BORDER}
${LDIR}/${TNAMENUM}.jpg
    else
      echo "sides"
      NEWWIDTH=$(( ${HEIGHT} * 4 / 3 ))
      BORDTOT=$(( ${NEWWIDTH} - ${WIDTH} ))
      BORDER=$(( ${BORDTOT} / 2 ))
      echo ${BORDER}
      convert ${CURIMG} -bordercolor Black -border ${BORDER}x0
${LDIR}/${TNAMENUM}.jpg
    fi
  else
    # Copy the file as-is
    cp ${CURIMG} ${LDIR}/${TNAMENUM}.jpg
    echo ""
  fi
}

# Script proper starts here
echo $*
DIRNAME=$1
echo New dir name is ${DIRNAME}
shift
if [ ! -d ${DIRNAME} ]; then
  mkdir ${DIRNAME}
fi

# Generate the modified images
CURNUM=0
for IMAGE in $*
do
  grab_image ${IMAGE} ${DIRNAME} ${CURNUM}
  CURNUM=`expr ${CURNUM} + 1`
done
---------------------

 Sincerely,

 Ray Ingles                                                 (313)
227-2317

 "In an illustrative recent study, subjects were asked their opinion
about
  a social welfare policy, which was described as being endorsed either
by
   Democrats or by Republicans. Although the subjects sincerely believed
   that their responses were based on the objective merits of the
policy,
   the major determinant of what they thought of the policy was in fact
  whether or not their favored political party was said to endorse it."
                - Paul Bloom and Deena Skolnick Weisberg
          (study at: http://content2.apa.org/journals/psp/85/5/808)
The contents of this e-mail are intended for the named addressee only. It contains information that may be confidential. Unless you are the named addressee or an authorized designee, you may not copy or use it, or disclose it to anyone else. If you received it in error please notify us immediately and then destroy it.



More information about the mdlug mailing list