#!/bin/bash
# GUI script to convert images automatically
# Version: 0.1
# Date:    13 April 2011
# Author:  Chema Martin
# http://cristalinux.blogspot.com/2011/04/batch-convert-images-with-imagemagick.html
# hacked by celeste:crystalfaery to change thumnail size to 320x240
# 2011-04-15 09:19:32+00:00

convert --version &> /dev/null
# Is Imagemagick installed?
if [[ $? -eq 0 ]]; then

  # Variable definition
  INDEX1=0
  INDEX2=0
  INDEX3=0

  # Welcome, define script scope
  zenity --question --title "Welcome to PiConvert!" --text "Howdy!\n\nThis script was designed to convert PNG image files into JPG format and create a thumbnail for each of them.\n\nFirst you will be asked to select the file(s) to be converted, then the location where the converted files should be saved to.\n\nClick YES to continue, NO to exit."

  # Continue or Exit?
  if [ $? -eq 0 ]; then
  
    # Select files to convert
    FILES="$(zenity --file-selection --title 'Select files to be converted...' --multiple)"
  
    # Select where to convert them to
    LOCATION="$(zenity --file-selection --title 'Save files where...?' --directory)"

    # Let's split the string FILES on "|" to extract the file names
    for f in $(seq 0 ${#FILES}); do
      # Reached end of string, special case.
      if [ $f -eq $[${#FILES}-1] ]; then
        # Convert PNG image into JPG and JPG thumbnail  
        convert ${FILES:$INDEX1:$[${#FILES}-$INDEX1]} "$LOCATION""/image$INDEX3.jpg"
        convert -resize 320x240 ${FILES:$INDEX1:$[${#FILES}-$INDEX1]} "$LOCATION""/image$INDEX3""_thumb.jpg"
      else
        # Convert PNG image into JPG and JPG thumbnail
        if [ ${FILES:$f:1} == "|" ]; then
          INDEX2=$f
          convert ${FILES:$INDEX1:$[$INDEX2-$INDEX1]} "$LOCATION""/image$INDEX3.jpg"
          convert -resize 320x240 ${FILES:$INDEX1:$[$INDEX2-$INDEX1]} "$LOCATION""/image$INDEX3""_thumb.jpg"
          INDEX1=$[$f+1]
          INDEX3=$[$INDEX3+1]    
        fi
      fi 
    done

    exit 0
  fi
else
  zenity --error --title "Imagemagick was not found!" --text "PiConvert cannot work unless Imagemagick is installed.\n\nPlease install Imagemagick before running PiConvert again."
  exit 0
fi


syntax highlighted by Code2HTML, v. 0.9.1