Skip to content

Instantly share code, notes, and snippets.

@carlynorama
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlynorama/021dd1110b14ddece300 to your computer and use it in GitHub Desktop.
Save carlynorama/021dd1110b14ddece300 to your computer and use it in GitHub Desktop.
This script parses an image that has a legend in the top left hand corner. If an area of the corner is green then the file gets copied to a new directory with the appropriate coordinates.
#!/bin/bash
#Reduce time by running process for just the part of the
#grid actually represented
Y1=$1
Y2=$2
X1=$3
X2=$4
#used in prefix generation
ZPAD=2
#folder files saved into
MYDIR="named_tiles"
#About the image being processed and where to look
#on the legend for the location information.
#How far from the top left corner
XOFFSET=193
YOFFSET=90
#How big of an area am I searching from that offset?
let KEYWIDTH=497-$XOFFSET
let KEYHEIGHT=210-$YOFFSET
#How many possible tiles represented in that region?
NUM_X_POS=16 #how many tiles wide?
NUM_Y_POS=8 #how many tiles tall?
#How big will each tiles representative area be?
let XMULTIPLIER=${KEYWIDTH}/${NUM_X_POS}
let YMULTIPLIER=${KEYHEIGHT}/${NUM_Y_POS}
#Look in the middle of that area.
let XOFFSET=XOFFSET+XMULTIPLIER/2
let YOFFSET=YOFFSET+YMULTIPLIER/2
#What color on the legend indicates where I am.
#This result will be based on the formating used below.
TRUECOLOR="136,216,0"
#Some variables needed to generate the route from I took if all
#my origin, which is not the top left.
#Will only work if the files are from the same day.
NL=$'\n'
MYROUTE="MY ROUTE:$NL"
ROUTEX=7 #if not far left tile, 0, then where?
ROUTEY=7 #if not top tile, 0, then where?
#The defaults if no variables are passed into the script.
if [[ -z $Y1 ]]; then Y1=0 ; fi
if [[ -z $Y2 ]]; then let Y2=${NUM_Y_POS}-1 ; fi
if [[ -z $X1 ]]; then X1=0 ; fi
if [[ -z $X2 ]]; then let X2=${NUM_X_POS}-1 ; fi
if [ ! -d ./${MYDIR} ]; then mkdir ./${MYDIR}; fi
#-------------- START LOOP ----------------------#
# If there are PNG files... get them.
if ls | grep png; then for p in *.png; do
echo "Processing $p"
found=0
#Begin row-col scan with first row.
for i in $(seq $Y1 $Y2); do
let YCORD=${YOFFSET}+${i}*${YMULTIPLIER}
#FOR ROUTE TEXT
let YLOC=ROUTEY-${i}
#Begin col scan with first col
for j in $(seq $X1 $X2); do
let XCORD=${XOFFSET}+${j}*${XMULTIPLIER}
#FOR ROUTE TEXT
let XLOC=ROUTEX-${j}
if [ "$XLOC" -ge 0 ]
then XDESC="E"
else XDESC="W"
fi
XLOC=${XLOC#-}
#The format of this line should match pattern of TRUECOLOR.
mycolor=$(convert $p[1x1+${XCORD}+${YCORD}] -format "%[fx:floor(255*u.r)],%[fx:floor(255*u.g)],%[fx:floor(255*u.b)]" info:-)
#echo "I look $mycolor at $XCORD, $YCORD"
if [ "$mycolor" == "$TRUECOLOR" ]; then
found=1
#FOR ROUTE TEXT
printf -v location "%0*dN, %0*d%s" $ZPAD $YLOC $ZPAD $XLOC $XDESC
MYROUTE="${MYROUTE}${location}$NL"
#echo "I am at $location"
#FILE RENAME AND MOVE
printf -v fileprefix "%0*dy%0*dx" $ZPAD $i $ZPAD $j
#copy the file while preserving timestamps
cp -p $p ./${MYDIR}/${fileprefix}_${p}
#echo "I am at $filename"
break 2
fi
done
done
#If I don't belong anywhere... Death images will trigger this.
if [ "$found" -lt 1 ]
then
cp -p $p ./${MYDIR}/HELP_${p}
fi
done
fi
#------------------ END LOOP ------------------------#
#Save the route to file.
#Will only work if complete sequence runs. That is a
#feature for now.
echo "$MYROUTE" > route.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment