Sunday, January 29, 2012

Public Service Announcement

Have You Seen Me?

Saturday, January 28, 2012

Resize a Lot of Pictures

The church web site I run has a box the displays random pictures from our photo album. The formatting of the page is easiest if these pictures are no wider than 530 pixels and no higher than 340 pixels. Of course most pictures are taken with a camera these days are a couple of thousand pixels wide, so each picture needs to be resized. You can do that by hand, of course, but I found it easiest to write a script. If you're running Linux, this uses the ImageMagick package, particularly the identify and convert programs. On a Mac, it uses sips, which has been available at least since 10.3. I suppose it could be modified for Windows or run under Cygwin, but I don't use Windows enough to make it worth my time.

Save the file as websize, make it executable, and save it to a directory in your path. Then run it with the command

websize [list of image files]

This will take a file named, say, picture1.jpg, and create a new file named picture1_web.jpg that fits into the box defined by MAXWIDTH and MAXHEIGHT. If the picture already fits into the box it will duplicate the file.

#! /bin/bash

# Resizes a picture or pictures to make sure it fits into the UPB
# frontpage box of MAXWIDTH pixels wide by MAXHEIGHT pixels tall,
# keeping the same format (PNG, JPEG, BMP, etc.)

# usage

# websize [pictures]

# If a picture is named "picturename.ext", the shrunken picture
#  will be named "picturename_web.ext".

# If a picture already fits into the website, it will simply be
#  copied with its new name.  This makes it easier to find the
#  pictures that should be posted on the web.

# Define the maximum dimensions of the picture

let MAXWIDTH=530
let MAXHEIGHT=340

# For Linux machines we need "convert" from the ImageMagick package.
#  We'll assume it's in the standard location:

# For Macs the standard program is sips, at least since 10.3

if [ -f /usr/bin/sips ]
then
    OS="Mac"
elif [ -f /usr/bin/convert ]
then
    OS="Linux"
else
    echo "Cannot find a program to do the file conversion"
    exit 1
fi

for PICTURE
do

if [ "$OS" = "Linux" ]
then

#   identify is also part of the ImageMagick package

    let WIDTH=`identify $PICTURE | sed "s/x/ /" | awk '{print $3}'`
else
    let WIDTH=`sips -g pixelWidth $PICTURE | tail -1 | awk '{print $2}'`
fi

# Need new picture name:

HEADER=${PICTURE%.*}
EXT=${PICTURE##*.}
NEWPICT=${HEADER}_web.$EXT

echo Converting $PICTURE to $NEWPICT

# Is the picture too wide?

if (( $WIDTH > $MAXWIDTH ))
then
    if [ "$OS" = "Linux" ]
    then
 convert -resize ${MAXWIDTH}x $PICTURE $NEWPICT
    else
 sips --resampleWidth $MAXWIDTH $PICTURE --out $NEWPICT
    fi
else
    cp $PICTURE $NEWPICT
fi

# Note that the new picture may still be too large:

if [ "$OS" = "Linux" ]
then
    let HEIGHT=`identify $NEWPICT | sed "s/x/ /" | awk '{print $4}'`
else
    let HEIGHT=`sips -g pixelHeight $NEWPICT | tail -1 | awk '{print $2}'`
fi

if (( $HEIGHT > $MAXHEIGHT ))
then
#   echo Resizing height of $PICTURE

    if [ "$OS" = "Linux" ]
    then
 convert -resize x$MAXHEIGHT $NEWPICT 1_${NEWPICT}
    else
 sips --resampleHeight $MAXHEIGHT $NEWPICT --out 1_$NEWPICT
    fi

    mv 1_${NEWPICT} $NEWPICT

fi

done

The obvious modification is to allow MAXWIDTH and MAXHEIGHT to be read from the command line, maybe using -w and -h flags. That's for another day.

Saturday, January 07, 2012

Joining a Group

In an effort to make Ubuntu less scary to users (i.e., more Mac/Windows like), Ubuntu 11.10 removed some graphical system administration utilities that were previously available. One of those was a called Users and Groups, which let you see groups and make changes to group memberships.

The new Ubuntu would rather you just forget groups, so it includes no graphical tools for handling them. Fortunately it's still Linux, so you can fix it up yourself. Liberian Geek has details, but the first hint (using groupmod) didn't work for me, so here's my modified version. In this example, I want to add myself to the group vboxusers, which should allow me to get USB access for my Virtualbox installation. More on that some other time. For now, return with us to those thrilling days of yesteryear, before the GUI:

  1. Look at the file /etc/group. Specifically, we want to see who belongs to the group vboxusers:
    $ grep vboxusers /etc/group
    vboxusers:x:125:
    
    Nobody, as we expected.
  2. Now to follow the Geek and add myself:
    sudo usermod -a -G vboxusers rchawk
    
  3. Finally, confirm the addition
    $ grep vboxusers /etc/group
    vboxusers:x:125:rcjhawk
    

You'll need to log out and log back in before the system will recognize the new group setting.