Created
March 7, 2013 07:39
-
-
Save mostlygeek/5106247 to your computer and use it in GitHub Desktop.
Wrote this little bash script to go though a directory of PNG files and find the smallest width/height for a DOM element that fits any image in a set. The set of images is made up of a "close", "open" and "stroke" image sprites.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
t01-close.png | |
t01-open.png | |
t01-stroke.png | |
t02-close.png | |
t02-open.png | |
t02-stroke.png | |
t03-close.png | |
t03-open.png | |
t03-stroke.png | |
t04-close.png | |
t04-open.png | |
t04-stroke.png | |
t05-close.png | |
t05-open.png | |
t05-stroke.png | |
t06-close.png | |
t06-open.png | |
t06-stroke.png | |
t07-close.png | |
t07-open.png | |
t07-stroke.png | |
t08-close.png | |
t08-open.png | |
t08-stroke.png | |
t09-close.png | |
t09-open.png | |
t09-stroke.png | |
t10-close.png | |
t10-open.png | |
t10-stroke.png | |
t11-close.png | |
t11-open.png | |
t11-stroke.png | |
t12-close.png | |
t12-open.png | |
t12-stroke.png | |
t13-close.png | |
t13-open.png | |
t13-stroke.png | |
t14-close.png | |
t14-open.png | |
t14-stroke.png | |
t15-close.png | |
t15-open.png | |
t15-stroke.png | |
t16-close.png | |
t16-open.png | |
t16-stroke.png | |
t17-close.png | |
t17-open.png | |
t17-stroke.png | |
t18-close.png | |
t18-open.png | |
t18-stroke.png | |
t19-close.png | |
t19-open.png | |
t19-stroke.png | |
t20-close.png | |
t20-open.png | |
t20-stroke.png |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# This saves me the work of having to go through each set of png files and find the max X, and max Y | |
# to create a DOM element that is big enough to hold all the images inside. | |
# | |
get_dimensions () | |
{ | |
for i in {1..20} | |
do | |
if [ $i -lt 10 ] | |
then | |
i="0$i" | |
fi | |
open="t$i-open.png" | |
stroke="t$i-stroke.png" | |
close="t$i-close.png" | |
# | |
# Explanation: | |
# 1. Use `file` command to extract the dimensions of the PNG | |
# credit: http://superuser.com/a/275507 | |
# | |
# > file t01-open.png | |
# t01-open.png: PNG image data, 178 x 116, 8-bit/color RGBA, non-interlaced | |
# | |
# 2. use `cut` to split on a comma and return the 2nd field | |
# 3. use `sed` to chop away spaces | |
# | |
open_xy=$(file $open | cut -d ',' -f 2 | sed 's/ //g') | |
stroke_xy=$(file $stroke | cut -d ',' -f 2 | sed 's/ //g') | |
close_xy=$(file $close | cut -d ',' -f 2 | sed 's/ //g') | |
# Cut out the X position, this time splitting on the "x" | |
open_x=$(echo $open_xy | cut -d 'x' -f 1) | |
stroke_x=$(echo $stroke_xy | cut -d 'x' -f 1) | |
close_x=$(echo $close_xy | cut -d 'x' -f 1) | |
# Cut the Y position | |
open_y=$(echo $open_xy | cut -d 'x' -f 2) | |
stroke_y=$(echo $stroke_xy | cut -d 'x' -f 2) | |
close_y=$(echo $close_xy | cut -d 'x' -f 2) | |
# Get the largest X and Y | |
# This is a cheap trick, we will `sort` to organize the numbers in reverse | |
# and just pop off the first one ... | |
maxX=$(echo -e "$open_x\n$stroke_x\n$close_x" | sort -n -r | head -1) | |
maxY=$(echo -e "$open_y\n$stroke_y\n$close_y" | sort -n -r | head -1) | |
echo "t$i $open_xy $stroke_xy $close_xy $maxX $maxY" | awk '{printf "%-5s %-10s %-10s %-10s %s x %s\n", $1, $2, $3, $4, $5, $6 }' | |
done | |
} | |
results=$(get_dimensions) | |
echo "Key,Open,Stroke,Close,Max Dimensions " | awk -F ',' '{printf "%-5s %-10s %-10s %-10s %s\n", $1, $2, $3, $4, $5}' | |
echo "-------------------------------------------------------" | |
echo "$results" | |
# Bash note, `echo $results` won't have any of the newlines... we need the quotes to | |
# preserve the \n | |
echo | |
echo "Stylus CSS Processor Output ... mmm copy/paste" | |
echo "-------------------------------------------------------" | |
for l in "$results" | |
do | |
echo "$l" | awk '{printf "#%s \n width %spx\n height %spx\n", $1, $5, $7}' | |
done | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Key Open Stroke Close Max Dimensions | |
------------------------------------------------------- | |
t01 178x116 172x81 192x97 192 x 116 | |
t02 104x93 93x81 102x89 104 x 93 | |
t03 98x69 96x52 102x58 102 x 69 | |
t04 121x94 94x88 121x101 121 x 101 | |
t05 123x82 118x60 130x73 130 x 82 | |
t06 97x94 71x85 72x89 97 x 94 | |
t07 114x90 112x75 115x81 115 x 90 | |
t08 102x107 81x89 102x106 102 x 107 | |
t09 126x98 114x69 130x80 130 x 98 | |
t10 74x109 70x92 74x100 74 x 109 | |
t11 112x214 115x207 113x222 115 x 222 | |
t12 105x120 83x95 104x115 105 x 120 | |
t13 111x98 111x68 112x80 112 x 98 | |
t14 69x103 71x86 73x91 73 x 103 | |
t15 106x118 85x88 106x95 106 x 118 | |
t16 112x214 115x207 113x222 115 x 222 | |
t17 83x71 79x56 79x61 83 x 71 | |
t18 94x83 81x69 92x74 94 x 83 | |
t19 71x66 67x56 74x58 74 x 66 | |
t20 79x94 75x81 76x86 79 x 94 | |
Stylus CSS Processor Output ... mmm copy/paste | |
------------------------------------------------------- | |
#t01 | |
width 192px | |
height 116px | |
#t02 | |
width 104px | |
height 93px | |
#t03 | |
width 102px | |
height 69px | |
#t04 | |
width 121px | |
height 101px | |
#t05 | |
width 130px | |
height 82px | |
#t06 | |
width 97px | |
height 94px | |
#t07 | |
width 115px | |
height 90px | |
#t08 | |
width 102px | |
height 107px | |
#t09 | |
width 130px | |
height 98px | |
#t10 | |
width 74px | |
height 109px | |
#t11 | |
width 115px | |
height 222px | |
#t12 | |
width 105px | |
height 120px | |
#t13 | |
width 112px | |
height 98px | |
#t14 | |
width 73px | |
height 103px | |
#t15 | |
width 106px | |
height 118px | |
#t16 | |
width 115px | |
height 222px | |
#t17 | |
width 83px | |
height 71px | |
#t18 | |
width 94px | |
height 83px | |
#t19 | |
width 74px | |
height 66px | |
#t20 | |
width 79px | |
height 94px |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment