Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a bigger image from a set of images with the files names 00y00x (can make a subset of a bigger set image)
#!/bin/bash
#creates a montage of images from a selection of
#properly named files. The top left is 0, 0. The bottom
#right is presumed to be 7, 15. A subset of the map
#can be built with the correct inputs to the script.
#REQUIRES IMAGEMAGICK
#------------------------------ INPUTS -----------------------------#
#the first parameter passed to the shell script
#will be the bigger of the two y coordinates
Y1=$1
#the second parameter passed to the shell script
#will be the smaller of the two y coordinates
Y2=$2
#the third parameter passed to the shell script
#will be the bigger of the two X coordinates
X1=$3
#the fourth parameter passed to the shell script
#will be the smaller of the two x coordinates
X2=$4
#------------------------------ CONFIG -----------------------------#
#what size should the tiles be?
WIDTH=1204
HEIGHT=862
#what is the size of the splice to the bottom?
BB=2
#how thick should the final border be around the whole image?
bordersize=100
#what color should the final border be?
COLOR=white
#In the file name, how much zeropadding is needed?
zpad=2
#What is the name of the sub directory to save the files?
MYDIR="prepped_tiles"
#What is the file type of the images being saved?
EXT="png"
#What should the defaults be if sub coordinates are passed
#during the script call?
if [[ -z $Y1 ]]; then Y1=0 ; fi
if [[ -z $Y2 ]]; then Y2=7 ; fi
if [[ -z $X1 ]]; then X1=0 ; fi
if [[ -z $X2 ]]; then X2=15 ; fi
#------------------------------ SETUP ------------------------------#
#Correct width and height to be inclusive.
#(0 to 7 is 8 tiles but 7-0=7)
let map_width=$X2-$X1+1
let map_height=$Y2-$Y1+1
#If the destination folder doesn't exist, make it.
if [ ! -d ./${MYDIR} ]; then mkdir ./${MYDIR}; fi
#Set datetime for map file name used below. Setting
#it up here means the file name will reflect when the
#script was run not when the file is created.
vardate=$(date +%d\-%m\-%Y\_%H.%M.%S)
#---------- PROCESS ALL THE PNGS IN THE CURRENT DIRECTORY ----------#
# processes all pngs, assumes they are all named correctly
if ls | grep png; then for p in *.png; do
echo "Processing $p"
convert \( $p -gravity south -crop ${WIDTH}x${HEIGHT}+0+0 +repage \) \
-background black -splice 0x${BB} \
./${MYDIR}/${p/.png}_stats.png
done
fi
#--------- CREATE WHITE BLANK SPACES FOR MISSING TILES ONLY --------#
#for each row
for i in $(seq $Y1 $Y2); do
#do each column
for j in $(seq $X1 $X2); do
#what is the file prefix we will be looking for?
printf -v fileprefix "%0*dy%0*dx" $zpad $i $zpad $j
if [ ! -e ./${MYDIR}/${fileprefix}* ] ; then
echo "Making white tile for $fileprefix"
fullname=./${MYDIR}/${fileprefix}_${COLOR}.${EXT};
convert -size ${WIDTH}x${HEIGHT} xc:${COLOR} ${fullname}
fi
done
done
#-------------------------- BUILD THE MAP --------------------------#
cd ./${MYDIR}
echo "Building Map"
montage *.png -mode concatenate -tile \
${map_width}x${map_height} ../map_${vardate}.png
#------------------------- TRIM AND BORDER -------------------------#
cd ../
echo "Cleaning up map"
#will need to leave off trim when top corner has a tile.
#look for white? trim only if no variables set?
convert \( map*.png -fuzz 1% -trim \) \
-bordercolor ${COLOR} -border ${bordersize}x${bordersize} +repage \
map_${vardate}_ready.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment