Skip to content

Instantly share code, notes, and snippets.

@chiqui3d
Last active March 25, 2021 19:58
Show Gist options
  • Save chiqui3d/1e8786203d045626e34938fc0d7793dd to your computer and use it in GitHub Desktop.
Save chiqui3d/1e8786203d045626e34938fc0d7793dd to your computer and use it in GitHub Desktop.
Resize and compress in Bulk with Google's image recommendations with the super ImageMagick
# Search for images in any directory where the script is executed $(pwd) and compress and resize them
# Change to your liking
# echo $1 # parameter
# echo $(pwd); # current directory where script is run
find "$(pwd)" \( -iname \*.jpg -o -iname \*.jpeg \) -print0 | while read -r -d $'\0' file; do
name="${file##*/}" # Name with extension
directory="${file%/*}" # Image directory without image name :)
newname="$(echo $name | sed 's/ /-/g' | awk '{print tolower($0)}')" # Image name without spaces and lower
# $finalname: New output path.
# $finalname: The resize folder has been added in the output path so as not to replace the original image,
# $finalname: Also I have added a prefix to the name that is related to my product.
finalname=$directory/resize/mamparas-oficina-$newname
mkdir -p "$directory/resize" # Make new resize directory
# convert is from imagemagick https://imagemagick.org/script/convert.php
# $file: Is the current image path from loop
convert "$file" -monitor -sampling-factor 4:2:0 -strip -interlace JPEG -colorspace sRGB -resize 1000 -compress JPEG -quality 80 "$finalname"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment