Skip to content

Instantly share code, notes, and snippets.

@Birchwell
Created October 18, 2015 00:31
Show Gist options
  • Save Birchwell/3311a9d63c21ca24d021 to your computer and use it in GitHub Desktop.
Save Birchwell/3311a9d63c21ca24d021 to your computer and use it in GitHub Desktop.
Convert images to many formats. Quality can be set. The converted images will be placed in a separate folder and the originals will be intact. Zenity must be installed.
#! /bin/sh
# Increment and count the number of files passed to the script
# for later use in the progress dialog box
n=1
# Ask for the destination graphic image format to change to
# Next line is entered all on one line
EXT=`zenity --title="Convert Image" --entry --text="Please
enter the image format to convert to (e.g. PNG, JPG, TIFF, WEBP etc.)"`
if [ $? == 1 ]; then
exit
fi
# Make sure the file extension is in lowercase text
declare -l EXT
EXT=$EXT
# Ask for the quality level of the copied images
# Next line is entered all on one line
QUAL=`zenity --title="Please enter quality" --entry --text="100
= Full Quality, 75 = 75% Quality, 50 = 50% Quality"`
if [ $? == 1 ]; then
exit
fi
# If the destination image format is PNG, set background to none
# (transparent) and set the quality levels, which differ from JPG
if [ $EXT == "png" ];
then
BACKGROUND="-background none"
QUALITY=$(($QUAL / 10))
else
BACKGROUND="-background white"
QUALITY="$QUAL"
fi
# Generate the new directory name, and set it to uppercase text
NEW_DIR="./"$EXT"-"$(date +"%m%d%y-%H%M%S")
declare -u NEW_DIR
NEW_DIR=$NEW_DIR
mkdir $NEW_DIR
# Pause for one second, just to be sure that everything that
# needs to be done has been done
sleep 1
# Cycle through the selected files, performing the conversion
# one file at a time
for file in $@; do
if [ ! -e $file ]; then
continue
fi
# Get just the filename, without the original file extension
name=$( echo $file | cut -f1 -d.)
# Convert the file and write it out to the destination file
# Next line is entered all on one line
convert -quality $QUALITY $BACKGROUND $file $NEW_DIR/${name}.$EXT
# Set up the information to display in the progress dialog box
echo $(($n * 100 / $#))
echo "# Processing file: $file"
let "n = n+1"
# Next line is entered all on one line
done | (zenity --progress --title "Converting to "$EXT"..." --percentage=0 --auto-close --auto-kill)
exit 0
@alo0oz
Copy link

alo0oz commented Jun 4, 2020

HI, using this is creating an empty folder. and no progress bar is showing either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment