Skip to content

Instantly share code, notes, and snippets.

@wonderbird
Last active September 7, 2023 09:07
Show Gist options
  • Save wonderbird/b210877179e95ef1145b9d9216167336 to your computer and use it in GitHub Desktop.
Save wonderbird/b210877179e95ef1145b9d9216167336 to your computer and use it in GitHub Desktop.
Rescale and compress PNG images
#!/bin/sh
#
# Rescale and compress PNG images.
#
# Source: https://gist.github.com/wonderbird/b210877179e95ef1145b9d9216167336
#
# For every image in all subfolders of SOURCE:
#
# - Rescale image to a width and height of at most 1024 px.
# - Reduce colors to 256 with an optimized palette.
# - Write output to the folder TARGET keeping the directory hierarchy.
#
# ATTENTION
#
# The script may cause damage, if `!` characters are in the names of your files
# or folders.
#
# PREREQUISITES
#
# The tools "magick" and "pngquant" must be in the PATH:
#
# - https://imagemagick.org/script/magick.php
# - https://pngquant.org/
#
# CAVEAT
#
# This software is quite untested and might harm the files of your harddisk.
# Make sure you have an up-to-date backup before using this software.
#
# USAGE: downscale_reduce.sh SOURCE TARGET
#
# Read all files from directory "SOURCE", rescale, reduce colors and write
# them to directory "TARGET".
#
# AUTHOR: Stefan Boos <kontakt@boos.systems>
#
SOURCE="$1"
TARGET="$2"
ABSOLUTE_SOURCE_DIR=$(realpath "$SOURCE")
ABSOLUTE_TARGET_DIR="$TARGET"
echo "$ABSOLUTE_SOURCE_DIR"
echo "$ABSOLUTE_TARGET_DIR"
if [ -d "$ABSOLUTE_TARGET_DIR" ]; then
echo "Target directory exists: $ABSOLUTE_TARGET_DIR"
exit 1
fi
mkdir -p "$ABSOLUTE_TARGET_DIR"
if [ ! -d "$ABSOLUTE_TARGET_DIR" ]; then
echo "Cannot create target directory: $ABSOLUTE_TARGET_DIR"
exit 1
fi
ABSOLUTE_TARGET_DIR=$(realpath "$TARGET")
echo "$ABSOLUTE_TARGET_DIR"
OLD_IFS=$IFS
IFS=$(printf '\n\b')
for RELATIVE_FILE in *.png; do
ABSOLUTE_SOURCE_FILE=$(realpath "$RELATIVE_FILE")
ABSOLUTE_TARGET_FILE=$(echo "$ABSOLUTE_SOURCE_FILE" | sed "s!$ABSOLUTE_SOURCE_DIR!$ABSOLUTE_TARGET_DIR!")
echo "$ABSOLUTE_TARGET_FILE"
ABSOLUTE_TARGET_PARENT_DIR=$(dirname "$ABSOLUTE_TARGET_FILE")
if [ ! -d "$ABSOLUTE_TARGET_PARENT_DIR" ]; then
mkdir -p "$ABSOLUTE_TARGET_PARENT_DIR"
fi
magick "$ABSOLUTE_SOURCE_FILE" -resize 1024 -quality 10 "$ABSOLUTE_TARGET_FILE"
pngquant 256 -- "$ABSOLUTE_TARGET_FILE"
TARGET_FILENAME=$(basename "$ABSOLUTE_TARGET_FILE" ".png")
mv -v "$ABSOLUTE_TARGET_PARENT_DIR/$TARGET_FILENAME-fs8.png" "$ABSOLUTE_TARGET_FILE"
done
IFS=$OLD_IFS
#
# LICENSE
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment