P29
This commit is contained in:
@@ -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!"
|
||||
@@ -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'"
|
||||
BIN
flower-field.jpg.webp
(Stored with Git LFS)
Normal file
BIN
flower-field.jpg.webp
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
flowering-rain.png.webp
(Stored with Git LFS)
Normal file
BIN
flowering-rain.png.webp
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
flowers-7.jpg.webp
(Stored with Git LFS)
Normal file
BIN
flowers-7.jpg.webp
(Stored with Git LFS)
Normal file
Binary file not shown.
Reference in New Issue
Block a user