From d096b774ca5f5eb045c2f6be110ddd3b8c52a09b Mon Sep 17 00:00:00 2001 From: "N0\\A" Date: Wed, 10 Jun 2026 11:25:50 +0200 Subject: [PATCH] P29 --- compare_and_delete_larger.sh | 56 ----------------- convert_to_webp.sh | 119 ----------------------------------- flower-field.jpg.webp | 3 + flowering-rain.png.webp | 3 + flowers-7.jpg.webp | 3 + 5 files changed, 9 insertions(+), 175 deletions(-) delete mode 100755 compare_and_delete_larger.sh delete mode 100755 convert_to_webp.sh create mode 100644 flower-field.jpg.webp create mode 100644 flowering-rain.png.webp create mode 100644 flowers-7.jpg.webp diff --git a/compare_and_delete_larger.sh b/compare_and_delete_larger.sh deleted file mode 100755 index 7ad4e4d..0000000 --- a/compare_and_delete_larger.sh +++ /dev/null @@ -1,56 +0,0 @@ -#!/bin/bash - -# Script to compare .png/.jpg with their .webp versions and delete the larger one -# Usage: ./compare_and_delete_larger.sh [directory] -# Default: current directory - -set -euo pipefail - -TARGET_DIR="${1:-.}" - -echo "Scanning for image files in: $TARGET_DIR" - -# Function to process a single image file -process_file() { - local original="$1" - local webp_file="${original}.webp" - - # Check if webp version exists - if [[ ! -f "$webp_file" ]]; then - echo "[SKIP] No webp version for: $original" - return 0 - fi - - # Get file sizes - local original_size - local webp_size - original_size=$(stat -c%s "$original" 2>/dev/null || echo "0") - webp_size=$(stat -c%s "$webp_file" 2>/dev/null || echo "0") - - echo "[COMPARE] $original ($(numfmt --to=iec $original_size)) vs $webp_file ($(numfmt --to=iec $webp_size))" - - # Compare sizes - if [[ "$original_size" -gt "$webp_size" ]]; then - echo "[DELETE] $original is larger, deleting..." - rm -v "$original" - elif [[ "$original_size" -lt "$webp_size" ]]; then - echo "[DELETE] $webp_file is larger, deleting..." - rm -v "$webp_file" - else - echo "[SKIP] Both files have the same size" - fi -} - -# Export the function for use with find -exec -Export_func() { - export -f process_file -} -Export_func - -# Find all .png and .jpg files (case insensitive) and process them -find "$TARGET_DIR" -type f \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | while IFS= read -r -d '' file; do - process_file "$file" -done - -echo "" -echo "Done!" diff --git a/convert_to_webp.sh b/convert_to_webp.sh deleted file mode 100755 index b32cbbc..0000000 --- a/convert_to_webp.sh +++ /dev/null @@ -1,119 +0,0 @@ -#!/bin/bash - -# Batch convert all PNG/JPG/JPEG to WebP -# Usage: ./convert_to_webp.sh -# Output: appends .webp to each filename (e.g., "file.png" -> "file.png.webp") -# This avoids filename collisions - you can clean up names later - -set -e - -echo "=== WebP Batch Conversion ===" -echo "Starting at: $(date)" -echo "" - -TOTAL_ORIGINAL=0 -TOTAL_WEBP=0 -CONVERTED=0 -SKIPPED=0 - -# Function to format bytes -format_bytes() { - local bytes=$1 - if (( bytes >= 1073741824 )); then - echo "$(echo "scale=2; $bytes / 1073741824" | bc) GB" - elif (( bytes >= 1048576 )); then - echo "$(echo "scale=2; $bytes / 1048576" | bc) MB" - elif (( bytes >= 1024 )); then - echo "$(echo "scale=2; $bytes / 1024" | bc) KB" - else - echo "$bytes B" - fi -} - -echo "Processing PNG files (lossless)..." -for f in *.png; do - [ -f "$f" ] || continue - - original_size=$(stat -c%s "$f") - TOTAL_ORIGINAL=$((TOTAL_ORIGINAL + original_size)) - - output="${f}.webp" - - # Skip if already converted - if [ -f "$output" ]; then - echo " SKIP (already exists): $f" - SKIPPED=$((SKIPPED + 1)) - continue - fi - - echo -n " Converting: $f ... " - cwebp -lossless "$f" -o "$output" > /dev/null 2>&1 - - if [ -f "$output" ] && [ -s "$output" ]; then - webp_size=$(stat -c%s "$output") - TOTAL_WEBP=$((TOTAL_WEBP + webp_size)) - savings=$((original_size - webp_size)) - pct=$(echo "scale=1; $savings * 100 / $original_size" | bc) - echo "DONE [$(format_bytes $original_size) -> $(format_bytes $webp_size) | ${pct}% saved]" - CONVERTED=$((CONVERTED + 1)) - else - echo "FAILED" - fi -done - -echo "" -echo "Processing JPG/JPEG files (quality 100)..." -for f in *.jpg *.jpeg; do - [ -f "$f" ] || continue - - original_size=$(stat -c%s "$f") - TOTAL_ORIGINAL=$((TOTAL_ORIGINAL + original_size)) - - output="${f}.webp" - - # Skip if already converted - if [ -f "$output" ]; then - echo " SKIP (already exists): $f" - SKIPPED=$((SKIPPED + 1)) - continue - fi - - echo -n " Converting: $f ... " - cwebp -q 100 "$f" -o "$output" > /dev/null 2>&1 - - if [ -f "$output" ] && [ -s "$output" ]; then - webp_size=$(stat -c%s "$output") - TOTAL_WEBP=$((TOTAL_WEBP + webp_size)) - savings=$((original_size - webp_size)) - pct=$(echo "scale=1; $savings * 100 / $original_size" | bc) - echo "DONE [$(format_bytes $original_size) -> $(format_bytes $webp_size) | ${pct}% saved]" - CONVERTED=$((CONVERTED + 1)) - else - echo "FAILED" - fi -done - -echo "" -echo "=== Summary ===" -echo "Converted: $CONVERTED files" -echo "Skipped: $SKIPPED files (already exist)" -echo "" -echo "Original total: $(format_bytes $TOTAL_ORIGINAL)" -echo "WebP total: $(format_bytes $TOTAL_WEBP)" - -if [ $TOTAL_ORIGINAL -gt 0 ]; then - SAVINGS=$((TOTAL_ORIGINAL - TOTAL_WEBP)) - PCT=$(echo "scale=1; $SAVINGS * 100 / $TOTAL_ORIGINAL" | bc) - echo "Space saved: $(format_bytes $SAVINGS) ($PCT%)" -fi - -echo "" -echo "Finished at: $(date)" -echo "" -echo "=== Git LFS Note ===" -echo "The .webp files are NOT added to git." -echo "To track them with Git LFS later, run:" -echo " git lfs track '*.webp'" -echo " git add .gitattributes" -echo " git add *.webp" -echo " git commit -m 'Add WebP versions'" diff --git a/flower-field.jpg.webp b/flower-field.jpg.webp new file mode 100644 index 0000000..1b4041f --- /dev/null +++ b/flower-field.jpg.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a689e25c39f7f8349472c28e1f3c34c405963e1fc7207b149e9c76781f3792ea +size 1955544 diff --git a/flowering-rain.png.webp b/flowering-rain.png.webp new file mode 100644 index 0000000..afeed60 --- /dev/null +++ b/flowering-rain.png.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9ee63542c34436a8d034a735e5f9bb8b2e1399d449df57edddc97482499345fa +size 2014926 diff --git a/flowers-7.jpg.webp b/flowers-7.jpg.webp new file mode 100644 index 0000000..7ddb6ce --- /dev/null +++ b/flowers-7.jpg.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:611e885a8bcd5fcbe56d5bb90e87d181ae121184b920c9194c8888f0a204f8f2 +size 1391560