From a028f3a0912478c5a828a9f5dc2d1da309d7fd63 Mon Sep 17 00:00:00 2001 From: oxeo0 Date: Sun, 23 Mar 2025 18:09:25 +0100 Subject: [PATCH 1/2] add webp compression script --- imagecompress.sh | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 imagecompress.sh diff --git a/imagecompress.sh b/imagecompress.sh new file mode 100644 index 0000000..9c309dd --- /dev/null +++ b/imagecompress.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +# Run before executing: +# sudo apt install webp parallel + +# Script will compress all images in directory given in the first parameter. +# JPEGs are lossy compressed, PNGs are losslessly compressed +# $ ./imagecompress.sh # current directory +# $ ./imagecompress.sh opsec/ # specified directory +# Already compressed WebP image will be kept, it only cares about image/png and image/jpeg mime. +# Compression commands are run in parallel using all available cores but it can take some time at first. + +# WARNING: +# cwebp by default removes all EXIF, XMP and ICC tags. +# Images rotated only through Exif tag can have wrong orientation after compression. +# However we shouldn't have too much Exif here anyways. + +DIR="${1:-$(pwd)}" + +calculate_size() { + du -sh --exclude='.git' "$1" | awk '{print $1}' +} + +initial_size=$(calculate_size "$DIR") + +echo "Updating JPEGs" + +# Lossy compression for JPEGs +find "$DIR" -print0 | parallel -0 ' + [[ $(file --mime-type -b "{}") == "image/jpeg" ]] && + cwebp "{}" -q 65 -m 6 -o "{}" +' + +echo "Updating PNGs" + +# Lossless compression for PNGs +find "$DIR" -print0 | parallel -0 ' + [[ $(file --mime-type -b "{}") == "image/png" ]] && + cwebp "{}" -q 100 -m 6 -lossless -o "{}" +' + +final_size=$(calculate_size "$DIR") +echo "Compressed $initial_size -> $final_size From 4fc124448cffab0813991238f7e683d2610c3cb6 Mon Sep 17 00:00:00 2001 From: oxeo0 Date: Sun, 23 Mar 2025 18:11:12 +0100 Subject: [PATCH 2/2] fix quote --- imagecompress.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imagecompress.sh b/imagecompress.sh index 9c309dd..e1ff897 100644 --- a/imagecompress.sh +++ b/imagecompress.sh @@ -40,4 +40,4 @@ find "$DIR" -print0 | parallel -0 ' ' final_size=$(calculate_size "$DIR") -echo "Compressed $initial_size -> $final_size +echo "Compressed $initial_size -> $final_size"