Created
June 15, 2023 21:05
-
-
Save mukaschultze/45ba21decde0857fc279316b3f931e41 to your computer and use it in GitHub Desktop.
Optimize PNG images using pngcrush
This file contains 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 | |
original_total=0 | |
final_total=0 | |
saved_total=0 | |
# Calculate original total size | |
while IFS= read -r -d '' file; do | |
size=$(stat -c "%s" "$file") | |
original_total=$((original_total + size)) | |
done < <(find . -name "*.png" -type f -print0) | |
# Perform parallel compression and recalculate sizes | |
find . -name "*.png" -type f -print0 | parallel -0 -j+0 ' | |
original_size=$(stat -c "%s" "{}") | |
pngcrush -brute -blacken -ow "{}" | |
compressed_size=$(stat -c "%s" "{}") | |
saved=$((original_size - compressed_size)) | |
echo "File: {}, Original Size: ${original_size} bytes, New Size: ${compressed_size} bytes, Saved: ${saved} bytes" | |
' | |
# Calculate final total size | |
while IFS= read -r -d '' file; do | |
size=$(stat -c "%s" "$file") | |
final_total=$((final_total + size)) | |
done < <(find . -name "*.png" -type f -print0) | |
saved_total=$((original_total - final_total)) | |
# Format total sizes into human-readable format | |
formatted_original_total=$(numfmt --to=iec --suffix=B "$original_total") | |
formatted_final_total=$(numfmt --to=iec --suffix=B "$final_total") | |
formatted_saved_total=$(numfmt --to=iec --suffix=B "$saved_total") | |
if [[ $final_total -lt 0 ]]; then | |
formatted_final_total="-${formatted_final_total#-}" | |
fi | |
if [[ $saved_total -lt 0 ]]; then | |
formatted_saved_total="-${formatted_saved_total#-}" | |
fi | |
# Log total sizes and storage saved | |
echo "Original Total Size: $formatted_original_total" | |
echo "Final Total Size: $formatted_final_total" | |
echo "Total Storage Saved: $formatted_saved_total" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment