2018-05-10 11:59:34 +02:00
#! /bin/bash
#########################################################
# #
# resizePictures.sh #
# #
# Author: Damien Broqua <contact@darkou.fr> #
# Github: https://github.com/Dbroqua/resizePictures #
# Licence: Apache License 2.0 #
# #
# Requirement: #
# - imagemagick #
# #
# Usage: #
# - create logo.png in ~/templates #
# - run resizePictures.sh in wanted directory #
# All jpg of the current folder wil be resized and #
# stored in <currentfolder>/resized #
# If cover.jpg found, file will be converted in other #
# size and without logo (for my wordpress theme). #
# #
#########################################################
# Your choices
MAXWIDTH = 800
MAXHEIGHT = 600
2020-05-13 17:47:13 +02:00
COVERWIDTH = 955
COVERHEIGHT = 450
2018-05-10 11:59:34 +02:00
# Defining some generic variables
COVERFILE = "cover.jpg"
2018-08-12 20:13:42 +02:00
COVERRATIO = ` bc -l <<< " ${ COVERWIDTH } / ${ COVERHEIGHT } " `
2018-05-10 11:59:34 +02:00
LOGO = ~/template/logo.png
2018-08-12 20:13:42 +02:00
IMAGERATIO = ` bc -l <<< " ${ MAXWIDTH } / ${ MAXHEIGHT } " `
2018-05-10 11:59:34 +02:00
DEST = 'resized'
2020-05-13 17:47:13 +02:00
DESTJPG = " ${ DEST } /JPG "
DESTPNG = " ${ DEST } /PNG "
2018-05-25 22:23:04 +02:00
OPTIONS = '-depth 8 -quality 90 -strip -interlace Plane'
2018-08-12 20:13:42 +02:00
BORDERWIDTH = 12
BORDERTHINWIDTH = 1
BORDERTHINPLACEMENT = 8
2018-05-10 11:59:34 +02:00
# Defining some colors for log()
RED = "\E[31m"
BLUE = "\E[44m"
GREEN = "\E[92m"
BOLD = "\033[4m"
RESET = "\033[0m"
# If destination does not exists, create it
2020-05-13 17:47:13 +02:00
if [ ! -d ${ DESTJPG } ] ; then
2021-06-11 12:02:08 +02:00
mkdir -p ${ DESTJPG }
2020-05-13 17:47:13 +02:00
fi
if [ ! -d ${ DESTPNG } ] ; then
2021-06-11 12:02:08 +02:00
mkdir -p ${ DESTPNG }
2018-05-10 11:59:34 +02:00
fi
# For each files
2018-05-10 17:59:52 +02:00
for image in { *.jpg,*.JPG} ; do
2018-08-12 20:13:42 +02:00
if [ " ${ image } " != "*.JPG" ] && [ " ${ image } " != "*.jpg" ] ; then
2018-05-25 22:23:04 +02:00
if [ ! -f " ${ image } " ] ; then
2018-08-12 20:13:42 +02:00
echo -e " ${ RED } # ${ image } is not a valid image file in ${ PWD } ${ RESET } "
continue
2018-05-10 11:59:34 +02:00
fi
2021-06-11 12:02:08 +02:00
if [ ` exiftool -Orientation -n " ${ image } " | cut -d":" -f2| cut -d' ' -f2` -eq 6 ] ; then
2020-05-13 17:47:13 +02:00
convert -auto-orient ${ image } ${ image }
fi
2018-08-12 20:13:42 +02:00
# Extract image dimensions
2020-02-23 10:45:37 +01:00
WIDTH = ` identify -format "%[fx:w]" " ${ image } " `
HEIGHT = ` identify -format "%[fx:h]" " ${ image } " `
2018-05-10 11:59:34 +02:00
2018-08-12 20:13:42 +02:00
# Compute image ratio
TMPIMAGERATIO = ` bc -l <<< " ${ WIDTH } / ${ HEIGHT } " `
2018-05-10 11:59:34 +02:00
2018-08-12 20:13:42 +02:00
# Define image size
NEWWIDTH = ${ WIDTH }
NEWHEIGHT = ${ HEIGHT }
2018-05-10 11:59:34 +02:00
2018-08-12 20:13:42 +02:00
# Compute new image size based on image type
if [ " ${ image } " = = " ${ COVERFILE } " ] ; then
# Compute temporary with or height to crop correctly image
2021-06-11 12:02:08 +02:00
if ( ( $( echo " ${ COVERRATIO } >= ${ TMPIMAGERATIO } " | bc -l) ) ) ; then
2018-08-12 20:13:42 +02:00
NEWHEIGHT = $(( ${ WIDTH } * ${ COVERHEIGHT } / ${ COVERWIDTH } ))
else
NEWWIDTH = $(( ${ HEIGHT } * ${ COVERWIDTH } / ${ COVERHEIGHT } ))
fi
else
# Compute temporary with or height to crop correctly image
RATIO = ` echo ${ IMAGERATIO } '<' ${ TMPIMAGERATIO } | bc -l`
if [ ${ RATIO } -eq 1 ] ; then
NEWWIDTH = ${ MAXWIDTH }
NEWHEIGHT = ` LC_NUMERIC = "en_US.UTF-8" printf "%.0f" $( bc -l <<< " scale=1; ${ HEIGHT } * ${ NEWWIDTH } / ${ WIDTH } " ) `
else
NEWHEIGHT = ${ MAXHEIGHT }
NEWWIDTH = ` LC_NUMERIC = "en_US.UTF-8" printf "%.0f" $( bc -l <<< " scale=1; ${ WIDTH } * ${ NEWHEIGHT } / ${ HEIGHT } " ) `
fi
fi
2018-05-10 11:59:34 +02:00
2018-08-12 20:13:42 +02:00
# Convert cover
if [ " ${ image } " = = " ${ COVERFILE } " ] ; then
echo -e " ${ GREEN } Converting cover file ${ RESET } "
2018-05-10 11:59:34 +02:00
2018-08-12 20:13:42 +02:00
# Crop image with the good ratio
2020-02-23 10:45:37 +01:00
convert -gravity Center -crop ${ NEWWIDTH } x${ NEWHEIGHT } +0+0 +repage " ${ image } " " /tmp/ ${ image } "
2018-05-10 11:59:34 +02:00
2018-08-12 20:13:42 +02:00
# Rezise cover and save it
2020-02-23 10:45:37 +01:00
convert ${ OPTIONS } -resize ${ COVERWIDTH } x${ COVERHEIGHT } " /tmp/ ${ image } " " ${ DEST } / ${ image } "
2018-05-10 11:59:34 +02:00
2021-11-13 09:33:01 +01:00
# Convert to webp also
convert " ${ DEST } / ${ image } " -quality 50 " ${ DEST } /cover.webp "
2018-08-12 20:13:42 +02:00
# Remove temporary file
2020-02-23 10:45:37 +01:00
rm " /tmp/ ${ image } "
2018-05-10 11:59:34 +02:00
else
2018-08-12 20:13:42 +02:00
echo -e " ${ RED } Converting image ${ image } ${ RESET } "
2018-10-07 18:32:35 +02:00
echo -e " -> ${ BLUE } Create JPG version ${ RESET } "
2018-08-12 20:13:42 +02:00
# Compute positions for black border
BORDERUPPERLEFT = $(( ${ BORDERWIDTH } / 2 )) ,$(( ${ BORDERWIDTH } / 2 ))
BORDERLOWERLEFT = $(( ${ BORDERWIDTH } / 2 )) ,$(( ${ NEWHEIGHT } - ${ BORDERWIDTH } / 2 ))
BORDERLOWERRIGHT = $(( ${ NEWWIDTH } - ${ BORDERWIDTH } / 2 )) ,$(( ${ NEWHEIGHT } - ${ BORDERWIDTH } / 2 ))
BORDERUPPERRIGHT = $(( ${ NEWWIDTH } - ${ BORDERWIDTH } / 2 )) ,$(( ${ BORDERWIDTH } / 2 ))
# Compute positions for white border
THINBORDERUPPERLEFT = ${ BORDERTHINPLACEMENT } ,${ BORDERTHINPLACEMENT }
THINBORDERLOWERLEFT = ${ BORDERTHINPLACEMENT } ,$(( ${ NEWHEIGHT } - ${ BORDERTHINPLACEMENT } ))
THINBORDERLOWERRIGHT = $(( ${ NEWWIDTH } - ${ BORDERTHINPLACEMENT } )) ,$(( ${ NEWHEIGHT } - ${ BORDERTHINPLACEMENT } ))
THINBORDERUPPERRIGHT = $(( ${ NEWWIDTH } - ${ BORDERTHINPLACEMENT } )) ,${ BORDERTHINPLACEMENT }
2021-06-11 12:02:08 +02:00
2018-08-12 20:13:42 +02:00
# resize and add border on image
convert -resize ${ NEWWIDTH } x${ NEWHEIGHT } \
2021-06-11 12:02:08 +02:00
" ${ image } " \
-fill transparent -stroke black -strokewidth ${ BORDERWIDTH } -draw " stroke-linecap square path 'M $(( ${ BORDERWIDTH } / 2 )) ,0 L ${ BORDERLOWERLEFT } L ${ BORDERLOWERRIGHT } L ${ BORDERUPPERRIGHT } L ${ BORDERUPPERLEFT } Z' " \
-fill transparent -stroke white -strokewidth ${ BORDERTHINWIDTH } -draw " stroke-linecap square path 'M ${ THINBORDERUPPERLEFT } L ${ THINBORDERLOWERLEFT } L ${ THINBORDERLOWERRIGHT } L ${ THINBORDERUPPERRIGHT } L ${ THINBORDERUPPERLEFT } Z' " \
" ${ DESTJPG } / ${ image } "
2018-08-12 20:13:42 +02:00
# Add logo on imagedow
2020-05-13 17:47:13 +02:00
composite ${ OPTIONS } -gravity SouthEast " ${ LOGO } " " ${ DESTJPG } / ${ image } " " ${ DESTJPG } / ${ image } "
2018-10-07 18:32:35 +02:00
# Create same version but in PNG and with box shadow
echo -e " -> ${ BLUE } Create PNG version ${ RESET } "
filename = $( basename -- " ${ image } " )
extension = " ${ filename ##*. } "
filename = " ${ filename %.* } "
convert -resize ${ NEWWIDTH } x${ NEWHEIGHT } \
2021-06-11 12:02:08 +02:00
" ${ image } " \
-fill transparent -stroke black -strokewidth ${ BORDERWIDTH } -draw " stroke-linecap square path 'M $(( ${ BORDERWIDTH } / 2 )) ,0 L ${ BORDERLOWERLEFT } L ${ BORDERLOWERRIGHT } L ${ BORDERUPPERRIGHT } L ${ BORDERUPPERLEFT } Z' " \
-fill transparent -stroke white -strokewidth ${ BORDERTHINWIDTH } -draw " stroke-linecap square path 'M ${ THINBORDERUPPERLEFT } L ${ THINBORDERLOWERLEFT } L ${ THINBORDERLOWERRIGHT } L ${ THINBORDERUPPERRIGHT } L ${ THINBORDERUPPERLEFT } Z' " \
\( +clone -background black -shadow 80x3+2+2 \) \
+swap -background transparent -layers merge +repage \
" ${ DESTPNG } / ${ filename } .png "
2018-10-07 18:32:35 +02:00
# Add logo on imagedow
2020-05-13 17:47:13 +02:00
composite ${ OPTIONS } -gravity SouthEast " ${ LOGO } " " ${ DESTPNG } / ${ filename } .png " " ${ DESTPNG } / ${ filename } .png "
2020-02-23 10:45:37 +01:00
2020-05-13 17:47:13 +02:00
convert " ${ DESTPNG } / ${ filename } .png " -type TrueColorAlpha -quality 90 -format PNG32 -background transparent -depth 8 -strip " ${ DESTPNG } / ${ filename } .png "
2021-11-13 09:33:01 +01:00
# Convert png file to webp file
convert " ${ DESTPNG } / ${ filename } .png " -quality 50 " ${ DESTPNG } / ${ filename } .webp "
2018-05-10 11:59:34 +02:00
fi
2018-08-12 20:13:42 +02:00
fi
2018-05-10 11:59:34 +02:00
done