Last active
April 3, 2022 23:46
-
-
Save patrolez/17e61301b17282dcc0b6d9a972650961 to your computer and use it in GitHub Desktop.
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 | |
# contact: patrolez | |
# | |
# requires 'imagemagic' | |
# In Ubuntu/Debian: | |
# sudo apt-get install imagemagick-6.q16 | |
# | |
TIMEOUT=20s | |
TIMEFORMAT=' Took %3lR' | |
(convert -version | grep 'ImageMagick 6.9') && echo "[OK] Has ImageMagic >= 6.9" || { echo "Error: No ImageMagic >= 6.9"; exit 1; } | |
(bash --version | grep 'bash, version 5') && echo "[OK] Has bash >=5.0" || { echo "Error: No bash >=5"; exit 1; } | |
USAGE=$(cat <<-EOF | |
USAGE: | |
To expand/spread/inflate/gap image: | |
"$0" --spread <file_path> | |
OR | |
"$0" -s <file_path> | |
To pack (remove gaps) from image: | |
"$0" --pack <file_path> | |
OR | |
"$0" -p <file_path> | |
Requires 'imagemagic' | |
In Ubuntu/Debian: | |
sudo apt-get install imagemagick-6.q16 | |
ADVANCED usage: | |
source "$0" | |
and then use "rplace_" prefixed functions | |
EOF | |
) | |
COLORMAP_PNG='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAECAMAAADYmc3mAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAdnJLH8AAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAGNQTFRFAAAAAAAAAHVvAJ6qAKNoAMx4AMzAJFCkNpDqSTrBUVJSUen0alz/bQAabUgvfu1WgR6fiY2QlLP/nGkmtErAvgA51NfZ3hB/5Kv//ziB/0UA/5mq/6gA/7Rw/9Y1//i4////CSpJ8wAAAAF0Uk5TAEDm2GYAAAA9SURBVAgdhcG7FYAgEACw3AF+eLr/ehY6AYWdOoJJ7BZNSuES+hCKEIrHnX7EZvKoqldKo+lOq1nTHenHB6xzCQfTBTu7AAAAAElFTkSuQmCC' | |
COLORMAP_GREYS='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAACCAYAAACOoybuAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAdnJLH8AAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAADBJREFUCNeVwcEJwCAABLCcwxTBBYRC/7Wvuv8uOoNJsOCfE/T7Ad94QasXSJLiwAYn1QQEDbzDWQAAAABJRU5ErkJggg' | |
function rplace_spread() | |
( | |
local SUFFIX="-spre" | |
set -euE | |
in_file="$1" | |
local extension="${in_file##*.}" | |
local filepath_without_ext="${in_file%.*}" | |
out_name="${filepath_without_ext}${SUFFIX}.${extension}" | |
touch -a "${out_name}" | |
print_status "$?" '%.0sCreated "%s" file!\n' | |
printf 'Processing - inflating; src: %s\n' "${in_file}" | |
time timeout --foreground -k 0 "$TIMEOUT" convert "$in_file" -background transparent \ | |
-gravity NorthWest -crop 1x0 +repage -splice 1x0 -gravity east -splice 1x0 +append \ | |
-gravity NorthWest -crop 0x1 +repage -splice 0x1 -gravity south -splice 0x1 -append \ | |
"${out_name}" | |
print_status "$?" | |
) | |
function rplace_pack() | |
( | |
local SUFFIX="-pack" | |
set -euE | |
in_file="$1" | |
local extension="${in_file##*.}" | |
local filepath_without_ext="${in_file%.*}" | |
out_name="${filepath_without_ext}${SUFFIX}.${extension}" | |
touch -a "${out_name}" | |
print_status "$?" '%.0sCreated "%s" file!\n' | |
printf 'Processing - pack; src: %s\n' "${in_file}" | |
time timeout --foreground -k 0 "$TIMEOUT" convert "$in_file" -background transparent \ | |
-gravity NorthWest -crop 3x0 +repage -chop 1x0 -gravity east -chop 1x0 +append \ | |
-gravity NorthWest -crop 0x3 +repage -chop 0x1 -gravity south -chop 0x1 -append \ | |
"${out_name}" | |
print_status "$?" | |
) | |
function rplace_approx_to_colormap() | |
( | |
local SUFFIX="-red" | |
local COLORMAP_IM=${2:-"inline:${COLORMAP_PNG}"} | |
set -euE | |
in_file="$1" | |
local extension="${in_file##*.}" | |
local filepath_without_ext="${in_file%.*}" | |
out_name="${filepath_without_ext}${SUFFIX}.${extension}" | |
touch -a "${out_name}" | |
print_status "$?" '%.0sCreated "%s" file!\n' | |
printf 'Processing - adjusting colors without dither; src: %s\n' "${in_file}" | |
convert "$in_file" +dither -remap "${COLORMAP_IM}" "${out_name}" | |
print_status "$?" | |
) | |
function rplace_approx_to_colormap_dither() | |
( | |
local SUFFIX="-red-dit" | |
local COLORMAP_IM=${2:-"inline:${COLORMAP_PNG}"} | |
set -euE | |
in_file="$1" | |
local extension="${in_file##*.}" | |
local filepath_without_ext="${in_file%.*}" | |
out_name="${filepath_without_ext}${SUFFIX}.${extension}" | |
touch -a "${out_name}" | |
print_status "$?" '%.0sCreated "%s" file!\n' | |
printf 'Processing - adjusting colors; src: %s\n' "${in_file}" | |
convert "$in_file" -dither FloydSteinberg -remap "${COLORMAP_IM}" "${out_name}" | |
print_status "$?" | |
) | |
function rplace_render_preview() | |
( | |
local SUFFIX="-prev" | |
set -euE | |
in_file="$1" | |
local extension="${in_file##*.}" | |
local filepath_without_ext="${in_file%.*}" | |
out_name="${filepath_without_ext}${SUFFIX}.${extension}" | |
touch -a "${out_name}" | |
print_status "$?" '%.0sCreated "%s" file!\n' | |
printf 'Processing - producing preview; src: %s\n' "${in_file}" | |
convert -bordercolor white -compose Copy -border 2 "${filepath_without_ext%-*}"* -background transparent +append "${out_name}" | |
print_status "$?" | |
) | |
function print_status() | |
{ | |
local OK_MSG=${2:-'Done:\n src: %s\n dst: %s\n'} | |
local ERR_MSG=${3:-'Error!\n'} | |
case "$1" in | |
0 ) printf "$OK_MSG" "${in_file}" "${out_name}"; return ;; | |
124 ) printf 'Timeout! Exceeded %s\n' "$TIMEOUT"; return ;; | |
* ) printf "$ERR_MSG" "${in_file}" "${out_name}"; return ;; | |
esac | |
} | |
function main() | |
{ | |
ARGS=( "$@" ) | |
while true; do | |
if [ "$#" -eq 0 ]; then break; fi | |
case "$1" in | |
-h | --help ) echo "$USAGE"; exit ;; | |
esac | |
shift | |
done | |
set -- "${ARGS[@]}" | |
while true; do | |
case "$1" in | |
-s | --spread ) { rplace_spread "$2"; }; break ;; | |
-p | --pack ) { rplace_pack "$2"; }; break ;; | |
* ) echo "$USAGE"; break ;; | |
esac | |
done | |
} | |
if ! (return 0 2>/dev/null) | |
then | |
main "$@" | |
else | |
echo '[OK] Sourced. Type "rplace_" for auto-completion' | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment