Last active
August 1, 2024 22:38
-
-
Save nyku/19f88344c8627907bdc08090c8a8b642 to your computer and use it in GitHub Desktop.
Resize & Convert image -> webp
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 | |
# Usage: | |
# ./resize_and_convert.sh ~/Desktop/folder-with-images | |
# Check if the correct number of arguments is provided | |
if [ "$#" -ne 1 ]; then | |
echo "Usage: $0 source_path" | |
exit 1 | |
fi | |
# Source directory from the terminal argument | |
INPUT_DIR="$1" | |
OUTPUT_DIR="${INPUT_DIR%/}-resized" | |
# Create the output directory if it doesn't exist | |
mkdir -p "$OUTPUT_DIR" | |
# Iterate over each image in the input directory | |
for image in "$INPUT_DIR"/*; do | |
# Get the filename without the extension | |
filename=$(basename -- "$image") | |
filename="${filename%.*}" | |
# Get the current width of the image | |
original_width=$(sips -g pixelWidth "$image" | awk '/pixelWidth/ {print $2}') | |
# Calculate the new width as 50% of the original width | |
new_width=$((original_width / 2)) | |
# Resize the image to 50% | |
sips --resampleWidth "$new_width" "$image" --out "$OUTPUT_DIR/${filename}.png" | |
# Convert the resized image to WebP format | |
cwebp "$OUTPUT_DIR/${filename}.png" -o "$OUTPUT_DIR/${filename}.webp" | |
# Remove the intermediate resized PNG file | |
rm "$OUTPUT_DIR/${filename}.png" | |
done | |
echo "Conversion complete. Resized images saved to $OUTPUT_DIR" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment