Created
May 1, 2020 11:12
-
-
Save dariowho/56ed60d4e1ec16349af3e348efecfac4 to your computer and use it in GitHub Desktop.
Generate JPEG thumbnails recursively, robust against whitespace in paths
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 | |
# Generate JPEG thumbnails recursively, robust against whitespace in paths | |
if [ "$1" == "--help" ] || [ "$1" == "-h" ] | |
then | |
echo "Usage: ./generate_thumbnails.sh SOURCE_DIR TARGET_DIR" | |
echo "Generate thumbnails from JPEG images in a given source folder, to a given target folder." | |
echo "Subfolders are visited recursively, and maintained in the target dir. Spaces in file names are allowed." | |
echo | |
exit | |
fi | |
if [ -z $1 ] || [ -z $2 ]; then echo "usage: generate_thumbs.sh source_dir/ target_dir/"; exit 1; fi | |
CONVERT_GEOMETRY="670" | |
FULLS_PATH=$1 | |
THUMBS_PATH=$2 | |
THUMBS_PATH_ABSOLUTE=`realpath "$THUMBS_PATH"` && | |
FULLS_PATH_ABSOLUTE=`realpath "$FULLS_PATH"` && | |
CWD=`pwd` && | |
if [ ! -d "$FULLS_PATH_ABSOLUTE" ]; then echo "Folder $FULLS_PATH_ABSOLUTE does not exist."; exit 1; fi | |
echo "Generating tumbnails for $FULLS_PATH_ABSOLUTE in folder $THUMBS_PATH_ABSOLUTE" | |
mkdir -p "$THUMBS_PATH" | |
cd $FULLS_PATH && | |
shopt -s globstar && | |
for source_path in ./**/*.jpg ./**/*.jpeg | |
do | |
source_relative_path=`realpath --relative-to=. "$source_path"` && | |
target_path="$THUMBS_PATH_ABSOLUTE/$source_relative_path" && | |
target_dir=`dirname "$target_path"` && | |
mkdir -p "$target_dir" | |
convert -quality 50 -thumbnail "x$CONVERT_GEOMETRY" "$source_path" "$target_path" | |
echo "Tumbnail generated for $source_relative_path" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment