Skip to content

Instantly share code, notes, and snippets.

@pimlie
Created August 30, 2017 12:21
Show Gist options
  • Save pimlie/0fad7489917b80316630fcf870a941a1 to your computer and use it in GitHub Desktop.
Save pimlie/0fad7489917b80316630fcf870a941a1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
img=$1
blocksize=15
img_width=300
img_height=300
w_steps=$(( img_width / blocksize ))
h_steps=$(( img_height / blocksize ))
colours=()
w_i=0
while (( w_i < w_steps )); do
h_i=0
while (( h_i < h_steps )); do
rgb=$(convert $img -crop $blocksize"x"$blocksize"+"$(( w_i*blocksize ))"+"$(( h_i*blocksize )) -resize 1x1 -format "%[fx:int(255*r+.5)],%[fx:int(255*g+.5)],%[fx:int(255*b+.5)]" info:-)
IFS=',' read -r -a rgba <<< $rgb
colour=$((1*${rgba[0]} + 1*${rgba[1]} + 1*${rgba[2]}))
colours[${#colours[@]}]=$colour" "$w_i"x"$h_i
h_i=$(( h_i + 1))
done
w_i=$(( w_i + 1))
done
IFS=$'\n' sorted=($(sort <<<"${colours[*]}"))
block=${sorted[0]/* /}
b_x=${block/x*/}
b_y=${block/*x/}
x=$(( b_x * blocksize + blocksize / 2 ))
y=$(( b_y * blocksize + blocksize / 2 ))
echo $x","$y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment