Skip to content

Instantly share code, notes, and snippets.

@elundmark
Created October 5, 2021 05:53
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 elundmark/1d57a1d81944f099560cf7fe6d94e102 to your computer and use it in GitHub Desktop.
Save elundmark/1d57a1d81944f099560cf7fe6d94e102 to your computer and use it in GitHub Desktop.
Slice image into grid, by width of number
#!/usr/bin/env bash
# Crop image using the CSS method top-right-bottom-left
# Uses get_tmp_hash.py
declare -i e_c=1 img_w=0 img_h=0 i=0 x=0 c_t=0 c_r=0 c_b=0 c_l=0 arglen=0 form_w=0 form_h=0 form_x=0 form_y=0 gui_input=0
declare -a args=() im_cmd=()
declare img mime_type jpeg_re='jpe?g' jpeg_quality tmp_file num_re='^[0-9]+$' output_name
parse_gui_args(){
eval "${*}" || exit 1
handle_num "$c_t"
handle_num "$c_b"
handle_num "$c_l"
handle_num "$c_r"
if (( c_t == 0 && c_b == 0 && c_l == 0 && c_r == 0)) ; then
exit 1
fi
return 0
}
handle_num(){
local num="${*}"
if [[ ! "$num" =~ $num_re ]] ; then
echo "invalid format: $num" 1>&2
exit 2
fi
return 0
}
while getopts ":t:r:b:l:a:o:g" options ; do
case "$options" in
a) handle_num "$OPTARG"; c_t=$OPTARG; c_b=$OPTARG; c_l=$OPTARG; c_r=$OPTARG;;
t) handle_num "$OPTARG"; c_t=$OPTARG;;
r) handle_num "$OPTARG"; c_r=$OPTARG;;
b) handle_num "$OPTARG"; c_b=$OPTARG;;
l) handle_num "$OPTARG"; c_l=$OPTARG;;
o) output_name="${OPTARG##**/}";;
g) gui_input=1;;
*) exit 1;;
esac
done
args+=( "${@:OPTIND}" )
arglen=${#args[@]}
if ((gui_input == 1)) ; then
if ! parse_gui_args "$(
rofi -p '' -mesg 'Crop cli arguments (eval):' -lines 9 -dmenu -i -width 50% -location 6 -disable-history \
<<< 'c_t=0; c_b=0; c_l=0; c_r=0;'
)"; then
exit 1
fi
fi
for (( ; x < arglen; x++ )); do
img="$(realpath "${args[x]}")"
if [[ $? -ne 0 || ! -f "$img" || ! -r "$img" ]] ; then
echo "file is invalid: $img" 1>&2
e_c=2
continue
fi
mime_type="$(file -b --mime-type "$img")"
if [[ ! "$mime_type" =~ ^image/ ]] ; then
echo "file is not an image: ${args[x]}" 1>&2
file -b --mime-type "$img" 1>&2
continue
fi
echo "$(basename "$img"): $mime_type" 1>&2
i=0
for n in $(identify -format "%w %h" "$img") ; do
i=$((i+1))
if ((i==1)) ; then
img_w=$n
elif ((i==2)) ; then
img_h=$n
fi
done
e_c=$?
if (( e_c != 0 || (img_w+img_h) == 0 )); then
echo ERROR 1>&2
e_c=1
continue
fi
echo -e "Width:\t${img_w}px" 1>&2
echo -e "Height:\t${img_h}px" 1>&2
# default values - diff. for every argument (image)
form_w=$img_w
form_h=$img_h
form_x=0
form_y=0
if ((c_t > 0)) ; then
form_h=$((form_h - c_t))
form_y=$c_t
fi
if ((c_l > 0)) ; then
form_w=$((form_w - c_l))
form_x=$c_l
fi
if ((c_r > 0)) ; then
form_w=$((form_w - c_r))
fi
if ((c_b > 0)) ; then
form_h=$((form_h - c_b))
fi
if [[ "$mime_type" =~ $jpeg_re ]] ; then
jpeg_quality="$(identify -format '%Q' "$img" || echo -n 100)"
if [[ ! "$jpeg_quality" =~ $num_re ]] || ((jpeg_quality < 1 || jpeg_quality > 100)) ; then
jpeg_quality=100
fi
im_cmd=( convert "$img" -quality $jpeg_quality )
else
im_cmd=( convert "$img" )
fi
tmp_file="$(dirname "$img" || echo -n /tmp)/$(get_tmp_hash.py 10)"
im_cmd+=(
'-crop'
"${form_w}x${form_h}+${form_x}+${form_y}"
"$tmp_file"
)
echo -n "Cropping (${form_w}x${form_h}+${form_x}+${form_y}) ... " 1>&2
"${im_cmd[@]}" 1>&2
if [[ $? -ne 0 || ! -f "$tmp_file" || ! -s "$tmp_file" ]] ; then
[[ $e_c -eq 0 ]] && e_c=1
echo "CROP COMMAND FAILED" 1>&2
nfy -i e crop.sh "CROP COMMAND FAILED"
[[ -f "$tmp_file" ]] && rm -f "$tmp_file" 1>&2
continue
else
touch -c -m --reference="$img" "$tmp_file"
echo OK 1>&2
fi
if [[ -z "$output_name" ]] ; then
output_name="crop-${form_w}x${form_h}+${form_x}+${form_y}-$(basename "$img")"
fi
mv --backup=numbered "$tmp_file" "$(dirname "$img")/$output_name" 1>&2
if [[ $? -ne 0 || ! -f "$img" || ! -s "$img" ]] ; then
[[ $e_c -eq 0 ]] && e_c=1
echo "ERROR"
continue
fi
echo "Cropped: $(identify -format "%G" "$img")" 1>&2
echo 1>&2
done
exit "$e_c"
#!/usr/bin/env python3
"""Random hash"""
import sys
import random
def get_rand_hash(n):
"""Random N character string"""
chars = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i",
"j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C",
"D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W",
"X","Y","Z"]
s = ""
i = 0
while i < n:
s += random.choice(chars)
i += 1
return s
print(get_rand_hash(int(sys.argv[1])))
#!/usr/bin/env bash
# This script uses crop.sh
# $ ./splice_img.sh -W 2 -H 2 %F // splits images into a 2x2 grid
# $ ./splice_img.sh -w 2 -w 2 %F // splits images of @10x10px into a grid of 25 imgs @2x2px
declare -i n=1 arglen=0
declare -i imgw=0 imgh=0 w=0 h=0 t=0 r=0 b=0 l=0 x=0 y=0 loopx=0 loopy=0 W=0 H=0
declare img img_ext format padw
declare -a args=()
while getopts ":w:h:W:H:" options ; do
case "$options" in
w) w=$OPTARG;; # Specify exact width per slice
h) h=$OPTARG;; # Specify exact height per slice
W) W=$OPTARG;; # Specify rows to split into
H) H=$OPTARG;; # Specify cols to split into
*) exit 1;;
esac
done
args+=("${@:OPTIND}")
arglen=${#args[@]}
for (( i = 0; i < arglen; i++ )); do
img="${args[i]}"
if [[ ! -f "$img" ]] ; then continue; fi
format="$(identify -format '%w'$'\t''%h' "$img")" || exit 5
imgw=$(cut -f1 <<< "$format")
imgh=$(cut -f2 <<< "$format")
if ((W > 0 && H > 0)) ; then
w=$(awk -v imgw="$imgw" -v W="$W" 'BEGIN{printf "%i\n", (imgw / W) + 1}')
h=$(awk -v imgh="$imgh" -v H="$H" 'BEGIN{printf "%i\n", (imgh / H) + 1}')
fi
loopx=$(awk -v imgw="$imgw" -v w="$w" 'BEGIN{printf "%i\n", (imgw / w) + 1}')
loopy=$(awk -v imgh="$imgh" -v h="$h" 'BEGIN{printf "%i\n", (imgh / h) + 1}')
if ((w <= 0 || h <= 0)) ; then
echo "No target dimensions specified" 1>&2
exit 4
fi
padw=$(( loopx * loopy ))
padw="${#padw}"
for ((y = 0; y < loopy; y++)); do
for ((x=0; x < loopx; x++)); do
l=$((x * w))
r=$((imgw - ((x+1) * w) ))
if ((r < 0)) ; then r=0; fi
t=$((y * h))
b=$((imgh - ((y+1) * h) ))
if ((b < 0)) ; then b=0; fi
# echo "x=${x}, y=${y} t=${t} r=${r} b=${b} l=${l}"
img_ext=".$(file --brief --extension "$img")" || exit 6
# echo "$img_ext"
crop.sh -o "$(
dirname "$img"
)/$(
basename "$img" "$img_ext"
)-slice-$(printf '%0*d\n' "$padw" $n)${img_ext}" -t "$t" -r "$r" -b "$b" -l "$l" "$img" || exit 4
n=$((n+1))
done
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment