#!/bin/bash
#
# resize_images():
# recursively scans directories and looks for image files.
# if found, resize the images using convert(1).
#
# this code by fdd is in the public domain and freely copyable.
# early devel stage - note to self: this could be made portable,
# i.e., not just for bmp, but for multiple image formats.
# to be forked later...
# recursively get a list of all the bmp files in the dir structure.
/usr/bin/find -name "*.bmp" | tee ./bmp_list
# bmp_id: wrapper for identify(1).
bmp_id() {
# run identify(1), w/ basic info.
# write to some file... append, maybe.
identify $1
# returns (in this order):
# file name, width and height, colormapped or not,
# no. of colors, number of bytes, image format,
# no. of seconds it took to read and process the image.
# let's say we are interested only in the image size:
#identify -verbose $i | grep Geometry: | grep [0-9]*x[0-9]* -o
}
# bmp_size: identify(1) wrapper, size grab.
bmp_size() {
# we're interested only in the size:
identify -verbose $1 | grep Geometry: | grep [0-9]*x[0-9]* -o
# prints out: "[height]x[width]".
}
# bmp_resize: resize using convert(1).
bmp_resize() {
BMP_SIZE=`bmp_size $1` # get size here.
# if the bmp is 64x64, resize it, w/ convert(1).
# strip down ".bmp" extension id in the filename.
file_base=`basename $1 .bmp`
file_base_ext=${file_base}-adaptive_128.bmp
echo $1
#echo -e $file_base
#echo -e $file_base_ext
echo -en "\t"
if [ "$BMP_SIZE" = "64x64" ]; then
echo "image is 64x64."
convert.exe "$1" -adaptive-resize 128x128 "./resized_images-adp/$file_base_ext"
convert.exe "$1" -resize 128x128 "./resized_images-reg/$file_base_ext"
else
# skip it.
echo "image is NOT 64x64."
fi
}
# output directories for this whole thing.
mkdir resized_images-adp # w/ `-adaptive-resize'.
mkdir resized_images-reg # w/ `-resize' (regular).
# for each bmp, run the local bmp_id() f'n.
# workaround skeleton:
#for i in `/usr/bin/find -name "*.bmp"`; do : function $i; done
# set IFS to newline, so as to be able to parse filenames containing spaces.
# rationale: see [1].
IFS=$'\n' # mandatory, if we use a bash flow control approach.
#for i in `/usr/bin/find -name "*.bmp"`; do bmp_id $i; done
for i in `/usr/bin/find -name "*.bmp"`; do bmp_resize $i; done
# output files list (for future use) [2].
/usr/bin/find -name "*.bmp" ./resized_images-adp/ > ./bmp-adp_list
/usr/bin/find -name "*.bmp" ./resized_images-adp/ > ./bmp-adp_list
# should IFS be unset now?...
# 07:15 PM Sun, March 31, 2013 - personal note.
# Rewrite this for JPEG texture files from Penumbra.
# Also... for Half-Life 2 textures. There should be some texture pack somewhere.
# [1] http://altair.uni.cx/growl/ifs.html
# [2] Used by mpeg7fex(1) as an input image file list.
#
### ## # eof. # ## ###.