Skip to content

Instantly share code, notes, and snippets.

@n-yoda
Last active August 29, 2015 14:18
Show Gist options
  • Save n-yoda/316fc6085798b5974c50 to your computer and use it in GitHub Desktop.
Save n-yoda/316fc6085798b5974c50 to your computer and use it in GitHub Desktop.
Optimize 9-patch images by shrinking inner region.
#!/bin/bash
#
# opt9p.sh
# Optimize 9-patch images by shrinking inner region.
# Copyright 2015 Nobuki Yoda.
#
# Usage:
# L=10 W=1 R=10 T=10 H=1 B=10 ./opt9p in.png out.png
#
# Simple Usage:
# LR=10 TB=10 ./opt9p in.png
#
# Params and Variables:
# -- files
# $1: input file
# $2: output file (default: _$1)
# -- borders
# $L: left border (default: $LR)
# $R: right border (default: $LR)
# $T: top border (default: $TB)
# $B: bottom border (default: $TB)
# -- border shortcuts
# $LR: left and right (default: 0)
# $TB: top and bottom border (default: 0)
# -- how to shrink inner region
# $W: result width of inner region (default: 1)
# $H: result height of inner region (default: 1)
#
# Dependency: ImageMagick
#
LR=${LR-${RL-0}}; L=${L-${LR}}; R=${R-${LR}}
TB=${TB-${BT-0}}; T=${T-${TB}}; B=${B-${TB}}
W=${W-1}; H=${H-1}
XD=0
if (( $W <= 0 )); then XD=$XD,2; W=1; fi
if (( $L <= 0 )); then XD=$XD,1; fi
if (( $R <= 0 )); then XD=$XD,3; fi
if (( $L <= 0 && $R <= 0 )); then W=100%; XD=1,2,3; fi
YD=0
if (( $H <= 0 )); then YD=$YD,2; H=1; fi
if (( $T <= 0 )); then YD=$YD,1; fi
if (( $B <= 0 )); then YD=$YD,3; fi
if (( $T <= 0 && $B <= 0 )); then H=100%; YD=1,2,3; fi
# run image magick
convert \( ${1} \) \
\( -clone 0 -gravity west -crop ${L}x+0 \) \
\( -clone 0 -crop +${L}+0 -crop -${R}+0 -resize ${W}x! \) \
\( -clone 0 -gravity east -crop ${R}x+0 \) \
-delete ${XD} +append \
\( -clone 0 -gravity north -crop x${T}+0+0 \) \
\( -clone 0 -crop +0+${T} -crop +0-${B} -resize x${H}! \) \
\( -clone 0 -gravity south -crop x${B}+0+0 \) \
-delete ${YD} -append ${2-_${1}}
@n-yoda
Copy link
Author

n-yoda commented Apr 9, 2015

引数を頑張ってパースしたくは無いので、Left Right Top Bottomは変数として渡しています。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment