Skip to content

Instantly share code, notes, and snippets.

@camallen
Created March 29, 2018 16:51
Show Gist options
  • Save camallen/21104a4793bec9e04211e0a2f12b6171 to your computer and use it in GitHub Desktop.
Save camallen/21104a4793bec9e04211e0a2f12b6171 to your computer and use it in GitHub Desktop.
DIRS=(local_image_directory)
for dir_to_process in "${DIRS[@]}" ; do
echo "converting files in $dir_to_process"
cd $dir_to_process
# possibly speed up using parallels? https://unix.stackexchange.com/questions/320877/how-to-use-convert-and-xargs-together
OUT_PATH="../converted/${dir_to_process}"
# this is from another project but I manually tested conversion of images to determine the following values
# resize to max width @ 2048 (match other sites) and 80% quality to get under 1M / 900K
# run some manual tests to see what works for you, e.g.
# convert image_file.jpg -resize 2048x -quality 90 test_output/image_file_2048_90.jpg
# convert image_file.jpg -resize 1024x -quality 70 test_output/image_file_1024_70.jpg
# etc
# the below relies on imagemagick convert tool
# https://www.imagemagick.org/script/convert.php
# https://serverfault.com/questions/194795/how-do-i-install-convert-on-a-linux-system
# find the jpegs and pass them to convert, note we pass in the outpath as positional param 2
find . -type f -name '*.JPG' -exec sh -a -c 'convert "$0" -resize 2048x -quality 80 "$1/${0}"' {} $OUT_PATH \;
cd ..
done
@camallen
Copy link
Author

Alternatively look at using https://squoosh.app/ or their CLI tool https://github.com/GoogleChromeLabs/squoosh/tree/dev/cli for image optimzation and resizing.

@camallen
Copy link
Author

Another options to batch-resize your photos, videos, or audio files, is https://ffmpeg.org/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment