I made an Image Resizer in bash which requires Imagemagick and Zenity.
USAGE:
Copy the following code and save it as a text file (e.g. resize_img.sh) and make it executable.
Code: Select all
#!/bin/bash
#
# resize_img.sh -- An Imagemagick Resize Script
#
#
# Author: canci <cancivolonter@gmail.com>
# License: GPL. See http://www.gnu.org/licenses/gpl.html
#
# Dependencies: Bash (duh!), Imagemagick, zenity
#
#
# 1. IFS and other delcarations
#
# Saving the old one
#
SAVEIFS=$IFS
# Declaring the new one
#
IFS=$(echo -en "\n\b")
# Files
#
FILES=`ls -1 $@`
# Directory name in case files are not being overwritten
#
NEW_DIR=RESIZED__`date +%d-%b-%Y_%T`
# Integer file count for the progress bar
#
n=1
# 2. Get desired resolution; Exit if cancel button pressed, otherwise move on
#
RES=`zenity --title="Please enter a resoultion" --entry --text="Image Resizer -- Please enter a resoultion (e.g. 800x600)"`
if [ $? == 1 ]; then
exit
else
# 2.1. Should the resize disregard the current aspect ratio and resize strictly to the given resolution?
#
Q1=`zenity --title="Disregard aspect ratio?" --question --text="Disregard aspect ratio? If Yes, the images will be resized to the exact resolution you provided, which might skew images if the aspect ratio differs. If No, the aspect ratio will be preserved and only the width will be resized accurately."`
if [ $? == 0 ]; then
# 2.1.1. Should the file be overridden or should a new one be created? mogrify or convert will be used accordingly.
#
Q2=`zenity --title="Overwrite file?" --question --text="Overwrite file? If No, the resized images will be put inside a new directory called RESIZED with the date of creation appended to it."`
if [ $? == 0 ]; then
for f in $FILES
do
mogrify -resize `echo $RES`! $f
echo $n
echo "# Processing file: $f"
let "n = n+1"
done | (zenity --progress --title "Resizing..." --percentage=0 --auto-close --auto-kill)
zenity --info --text="Done!"
else
mkdir $NEW_DIR
for f in $FILES
do
convert -resize `echo $RES`! $f $NEW_DIR/$f
echo $n
echo "# Processing file: $f"
let "n = n+1"
done | (zenity --progress --title "Resizing..." --percentage=0 --auto-close --auto-kill)
zenity --info --text="Done!"
fi
# WHAT ELSE? HUH?
else
# 2.1.2. Same as in 2.1.1., but for aspect ratio resize.
#
Q3=`zenity --title="Overwrite file?" --question --text="Overwrite file? If No, the resized images will be put inside a new directory called RESIZED with the date of creation appended to it."`
if [ $? == 0 ]; then
for f in $FILES
do
mogrify -resize $RES $f
echo $n
echo "# Processing file: $f"
let "n = n+1"
done | (zenity --progress --title "Resizing..." --percentage=0 --auto-close --auto-kill)
zenity --info --text="Done!"
else
mkdir $NEW_DIR
for f in $FILES
do
convert -resize $RES $f $NEW_DIR/$f
echo $n
echo "# Processing file: $f"
let "n = n+1"
done | (zenity --progress --title "Resizing..." --percentage=0 --auto-close --auto-kill)
zenity --info --text="Done!"
fi
# Ending 2.1.
fi
# Ending 2
fi
# 3. Restore IFS (for possible future use)
#
IFS=$SAVEIFS
# EOF
exit 0
1. what resolution you want
2. whether you want to resize according to aspect ratio or if you want to disregard that
3. if you want to overwrite the files or make new ones in a subdirectory called RESIZED
4. it will show a nice progress bar
It's meant to be called as a custom action from Thunar (please use %N, not %F, or else it won't work!; instructions here) or Nautilus (no clue how it's installed there, so, if anyone can help?), where you would mark the files, right click and call the script via a custom action. You could mark one, or several. It does not work on directories though, meaning that it won't consider files inside directories. But you can just call it from the command line, with just one file or with many:
Code: Select all
resize_img.sh cow.png
Code: Select all
resize_img.sh *
Code: Select all
resize_img.sh 1.jpg 2.jpg foo.png
Code: Select all
resize_img.sh *.jpg
Code: Select all
resize_img.sh screenshot*
etc.
The code is GPL.
I hope you'll enjoy it.
CHANGELOG
v0.2 --- 3rd Feb 2011 --- If you choose not to overwrite your files, a directory is created with the name RESIZED_$DATE to prevent the script from overwriting images in an existing RESIZED directory (Big thanks to nadir for this suggestion).