# Resize Images In This Folder - Simple function to resize images .
# Resource : http://www.imagemagick.org/script/command-line-processing.php#geometry
# Resource : https://unix.stackexchange.com/questions/129391/passing-named-arguments-to-shell-scripts
function riitf(){
local ImageFindingRegex='\./.+\.(?:jpg|JPG|jpeg|JPEG|png|PNG)$';
local ResizedImageQuality="90%";
local ResizedImageIsCropped=0
while getopts "g:q:c" opt; do
case $opt in
g) local ResizedImageGeometryOptions="$OPTARG"
;;
c) local ResizedImageIsCropped=1
;;
q) local ResizedImageQuality="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done
echo
local ResizedImageFolder="_resized/${ResizedImageGeometryOptions}";
if [ -v "$ResizedImageGeometryOptions"]
then
local ResizedImageGeometryOptions=2000x;
fi
echo "- Starting 'Resize Images In This Folder'";
echo -e "|- Resized Image Geometry Options = ${ResizedImageGeometryOptions}";
echo -e "|- Resized Image Quality = ${ResizedImageQuality}";
echo -e "|- Resized Image Is Cropped = ${ResizedImageIsCropped}";
echo -e "|- Resized Image Folder = ${ResizedImageFolder}";
echo "|- Resizing the following images :";
mkdir -p $ResizedImageFolder;
if [ "$ResizedImageIsCropped"==0 ]
then
find . -maxdepth 1 -type f \
-regextype 'egrep' -iregex ${ImageFindingRegex} \
-exec echo " |- {} " \; \
-exec convert "{}" -resize "${ResizedImageGeometryOptions}" -quality ${ResizedImageQuality} \
-set filename:f \'%[f]-%[t]-%[w]x%[h].%[e]\' "${ResizedImageFolder}/%[filename:f]" \;
else
find . -maxdepth 1 -type f \
-regextype 'egrep' -iregex ${ImageFindingRegex} \
-exec echo " |- {} " \; \
-exec convert "{}" -crop "${ResizedImageGeometryOptions}" -quality ${ResizedImageQuality} \
-set filename:f \'%[f]-%[t]-%[w]x%[h].%[e]\' "${ResizedImageFolder}/%[filename:f]" \;
fi
echo " ... executing 'bash' as a workaround for function variables not resetting after function execution !!!";
bash
}
Imagemagick Usage Guide & Examples
Working Example : Resize Images In This Folder A.K.A RIITF
Adding the code blow into a script or bash aliases then executing riitf()
will resize all images in the current folder .
Failed attempts for refrence.
Trying to use created date in filname !.
for f in *.JPG; do
mkdir -p ./_temp;
printf "Resizing $f\n started ...";
convert "$f" -resize 1200x -quality 80% -set filename:f '%[f]-%[t]-%[w]x%[h].%[e]' _temp/'%[filename:f]'
printf "Resizing $f\n complete.";
done
for f in *.JPG; do
img_date=$(identify -format "%[EXIF:DateTime]" $f);
printf "${img_date}";
image_date_simple=$(date -f "%Y:%m:%d %H:%M:%S" "$img_date" +"%Y-%m-%d");
printf "${image_date_simple}";
printf "Resizing $f\n started ...";
convert "$f" -resize 1200x -quality 80% -set filename:f '${image_date_simple}-%[t]-%[w]x%[h].%[e]' _temp/'%[filename:f]'
printf "Resizing $f\n complete.";
done
find . -maxdepth 1 -name "*.jpg" -exec convert -resize 1200x -quality 80% -set filename:f '%[t]-%[w]x%[h].%[e]' {} temp/'%[filename:f]' \;
mogrify -path . -resize 1920x1920 -format "_resized-1920x1920.jpg" -quality 80 *.jpg
convert "*.jpg" -resize 1200x1200 -set filename:new "%t_w1200" %[filename:new].jpg
mogrify -path ./temp -resize 1920x -set filename:f '%[t]-w%[w]h%[h].%[e]' -format "%[filename:f]" -quality 80 ./*.{jpg,png}
find . -maxdepth 1 -type f -regextype 'egrep' -iregex '\./.+\.(?:jpg|JPG|jpeg|JPEG|png|PNG)$' \
-exec \
printf "Resizing ${}\n started ..." && \
convert "{}" -resize 1200x -quality 80% -set filename:f '%[f]-%[t]-%[w]x%[h].%[e]' _temp/'%[filename:f]' &&
printf "Resizing ${}\n complete." \
\;
Useful Links
-
Imagemagic Website : http://www.imagemagick.org/script/command-line-processing.php
-
Sample ImageMagic wrapper on github: