Skip to content

Instantly share code, notes, and snippets.

@bdone
Forked from deekayen/generate_thumbnails.sh
Created March 1, 2013 22:04
Show Gist options
  • Save bdone/5068275 to your computer and use it in GitHub Desktop.
Save bdone/5068275 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Proper function of this script on Mac requires:
# install tiff 3.6.1 or newer http://www.libtiff.org/
# brew install ghostscript
# brew install imagemagick
timer()
{
if [[ $# -eq 0 ]]; then
echo $(date '+%s')
else
local stime=$1
etime=$(date '+%s')
if [[ -z "$stime" ]]; then stime=$etime; fi
dt=$((etime - stime))
ds=$((dt % 60))
dm=$(((dt / 60) % 60))
dh=$((dt / 3600))
printf '%d:%02d:%02d' $dh $dm $ds
fi
}
T=$(timer)
find sku_images -name '*.1' | while read f
do
error=""
filename="$(basename "$f" ".1")"
# Filenames could conflict since cropping duplicates file outputs.
# When faced with 1032522.1 and 1032522t.1, the former is used, second ignored.
if [ ! -f "sku_jpg/$filename.jpg" ]; then
# A grep for "TRUEVISION-XFILE" is not reliable to discover TGA.
# Default to TGA since that is the standard format and doens't
# necessarily have any binary identifiers to grep for.
# Note, "Paint Shop Pro" can also appear in TGA files.
error=$(convert -quiet -quality 100 -alpha off -density 200x200 -comment "Converted and resized using Imagemagick 6.8.0 from TGA format by Classic Graphics." -resize '100x100>' tga:"sku_images/$filename.1" "sku_jpg/$filename.jpg" 2>&1 >/dev/null)
# An error with this message probably means alternate formatting, not TGA.
# convert: improper image header
if [[ $error =~ "improper image header" ]]; then
if [[ $(grep -ce "^CPT7FILE" "$f") -ne 0 ]]; then
# grep "CPT7FILE" for Corel Draw files. see 1333859.1
echo "$filename.1 is an unsupported Corel Photo-Paint file and was not converted."
elif [[ $(grep -ce "Paint Shop Pro Image File" "$f") -ne 0 ]]; then
# grep "Paint Shop Pro Image File" for unsupported PSP. see 2026216.1
echo "$filename.1 is an unsupported Paint Shop Pro file and was not converted."
elif [[ $(grep -ce "^GIF89a" "$f") -ne 0 ]]; then
# grep at the file head for "GIF89a". see 2156309.1
convert -quiet -quality 100 -alpha off -density 200x200 -comment "Converted and resized from GIF89a using Imagemagick 6.8.0 by Classic Graphics." -resize '100x100>' gif:"sku_images/$filename.1" "sku_jpg/$filename.jpg"
elif [[ $(grep -ce "^II" "$f") -ne 0 ]]; then
# grep "II" meaning TIFF. see 2602091.1
convert -quiet -quality 100 -alpha off -density 200x200 -comment "Converted and resized from TIFF using Imagemagick 6.8.0 by Classic Graphics." -resize '100x100>' tiff:"sku_images/$filename.1" "sku_jpg/$filename.jpg"
elif [[ $(grep -ce "^BM" "$f") -ne 0 ]]; then
# grep "BM" at the start of the file to detect BMP. see 2041952.1
convert -quiet -quality 100 -alpha off -density 200x200 -comment "Converted and resized from BMP using Imagemagick 6.8.0 by Classic Graphics." -resize '100x100>' bmp:"sku_images/$filename.1" "sku_jpg/$filename.jpg"
else
# grep "JFIF" to discover JPEG files. see 0618015.1
# also look for for "LEAD Technologies"
# or "ˇÿˇ" at the head of the file
# JFIF is also found in PSP files, so this must be after that check.
# JFIF and LEAD aren't always in JPEG files.
# grep has trouble with "ˇÿˇ"
# Just try it and output if there's an error.
error=$(convert -quiet -quality 100 -alpha off -density 200x200 -comment "Resized using Imagemagick 6.8.0 by Classic Graphics." -resize '100x100>' jpeg:"sku_images/$filename.1" "sku_jpg/$filename.jpg" 2>&1 >/dev/null)
if [[ $error -ne "" ]]; then
echo "$filename.1 is an unknown filetype and was not converted."
fi
fi
fi
fi
done
printf 'Elapsed time: %s\n' $(timer $T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment