Last active
January 17, 2022 10:51
-
-
Save mrWeiss0/5f03875177c7a14b3486ae99da868db6 to your computer and use it in GitHub Desktop.
Converts and scales the files in a directory using ImageMagick
This file contains hidden or 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
:: USAGE: ./resize.bat [OPTION]... [DIR] | |
:: | |
:: Converts and scales the files in the directory DIR | |
:: If DIR is omitted it is asked to the user | |
:: Requires ImageMagick | |
:: | |
:: /q Runs quietly | |
:: /n No action, print file names only | |
:: | |
:: Parameters: | |
:: IEXT Extension of files to convert | |
:: OEXT Extension of converted files | |
:: SIZE Target size. Documented at | |
:: https://imagemagick.org/script/command-line-processing.php#geometry | |
:: QUAL Target quality, for JPEG ranges from 1 to 100. Documented at | |
:: https://imagemagick.org/script/command-line-options.php#quality | |
:: ODIR Output directory located in DIR | |
:: If not present it is created | |
@echo off | |
Setlocal EnableDelayedExpansion | |
:: Parameters | |
set IEXT=jpg | |
set OEXT=jpg | |
set SIZE=800x800 | |
set QUAL=90 | |
set ODIR=resized | |
set QUIET=0 | |
set NONO=0 | |
set MAGICK=magick | |
goto :begin | |
:: Executes ImageMagick. This is called for each image found | |
:: First argument is the name of the file without extension | |
:magick-cmd | |
%MAGICK% convert -quiet "%~1.%IEXT%[0]" -resize %SIZE% -quality %QUAL% "%ODIR%\%~1.%OEXT%" | |
exit /B | |
:: Logs a message except if running in quiet mode | |
:log | |
if %QUIET% equ 0 ( | |
set msg=%* | |
if "!msg!"=="" ( | |
echo/ | |
) ^ | |
else ( | |
echo !msg! | |
) | |
) | |
exit /B | |
:begin | |
:: Parse options | |
:arg-begin | |
set arg=%1 | |
if "!arg!"=="" goto :arg-end | |
if not "!arg:~0,1!"=="/" goto :arg-end | |
if "%arg%"=="/q" ( | |
set QUIET=1 | |
) ^ | |
else if "%arg%"=="/n" ( | |
set NONO=1 | |
) ^ | |
else ( | |
echo Invalid option %arg% >&2 | |
exit /B 1 | |
) | |
shift | |
goto :arg-begin | |
:arg-end | |
:: Check if ImageMagick is present | |
if %NONO% equ 0 ( | |
where %MAGICK% >NUL 2>NUL | |
if !ERRORLEVEL! neq 0 ( | |
echo ImageMagick not present on the system >&2 | |
exit /B 9009 | |
) | |
) | |
:: Log | |
call :log Resize to %SIZE% | |
call :log From %IEXT% to %OEXT% | |
call :log Quality %QUAL% | |
call :log Export to %ODIR%\ | |
:: Ask source directory if argument is omitted | |
set arg=%~1 | |
if "!arg!"=="" ( | |
set /p IDIR="Directory: " | |
) ^ | |
else ( | |
set IDIR=!arg! | |
call :log Directory: !arg! | |
) | |
call :log | |
:: Check if IDIR exists | |
if not exist "%IDIR%\" ( | |
echo %IDIR% is not a directory >&2 | |
exit /B 1 | |
) | |
cd "%IDIR%" || exit /B | |
:: Find files to convert | |
set FILES_COUNT=0 | |
for %%f in (*.%IEXT%) do ( | |
set FILES[!FILES_COUNT!]=%%~nf | |
set /A FILES_COUNT+=1 | |
) | |
call :log Found %FILES_COUNT% files | |
if %FILES_COUNT% equ 0 exit /B 0 | |
if %NONO% equ 0 ( | |
mkdir %ODIR% 2>NUL | |
) ^ | |
else ( | |
call :log No action | |
) | |
call :log | |
:: Padding | |
set FILES_COUNT_LEN=0 | |
set FILES_COUNT_PAD= | |
:pad-loop | |
if "!FILES_COUNT:~%FILES_COUNT_LEN%!"=="" goto :pad-loop-end | |
set /A FILES_COUNT_LEN+=1 | |
set FILES_COUNT_PAD=0%FILES_COUNT_PAD% | |
goto :pad-loop | |
:pad-loop-end | |
set FILES_COUNT_PAD=%FILES_COUNT_PAD:~-1% | |
:: Execute | |
set /A to=FILES_COUNT-1 | |
for /L %%i in (0,1,%to%) do ( | |
set f=!FILES[%%i]! | |
set /A c=%%i+1 | |
set c=%FILES_COUNT_PAD%!c! | |
call :log !c:~-%FILES_COUNT_LEN%!/%FILES_COUNT% !f!.%IEXT% | |
if %NONO% equ 0 call :magick-cmd "!f!" | |
) |
This file contains hidden or 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
#!/usr/bin/env bash | |
# USAGE: ./resize.sh [OPTION]... [DIR] | |
# | |
# Converts and scales the files in the directory DIR | |
# If DIR is omitted it is asked to the user | |
# Requires ImageMagick | |
# | |
# -q Runs quietly | |
# -n No action, print file names only | |
# | |
# Parameters: | |
# IEXT Extension of files to convert | |
# OEXT Extension of converted files | |
# SIZE Target size. Documented at | |
# https://imagemagick.org/script/command-line-processing.php#geometry | |
# QUAL Target quality, for JPEG ranges from 1 to 100. Documented at | |
# https://imagemagick.org/script/command-line-options.php#quality | |
# ODIR Output directory located in DIR | |
# If not present it is created | |
# Parameters | |
IEXT=jpg | |
OEXT=jpg | |
SIZE=800x800 | |
QUAL=90 | |
ODIR=resized | |
QUIET=0 | |
NONO=0 | |
MAGICK=convert | |
# Executes ImageMagick. This is called for each image found | |
# First argument is the name of the file without extension | |
function magick-cmd { | |
$MAGICK -quiet "$1.$IEXT[0]" -resize $SIZE -quality $QUAL "$ODIR/$1.$OEXT" | |
} | |
# Logs a message except if running in quiet mode | |
function log { | |
[ $QUIET -eq 0 ] && echo "$@" | |
} | |
# Parse options | |
while getopts ":qn" opt; do | |
case ${opt} in | |
q ) QUIET=1 | |
;; | |
n ) NONO=1 | |
;; | |
\? ) echo Invalid option -$OPTARG >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
# Check if ImageMagick is present | |
if [ $NONO -eq 0 ]; then | |
if ! command -v $MAGICK > /dev/null; then | |
echo ImageMagick not present on the system >&2 | |
exit 127 | |
fi | |
fi | |
# Log | |
log From $IEXT to $OEXT | |
log Resize to $SIZE | |
log Quality $QUAL | |
log Export to $ODIR/ | |
# Ask source directory if argument is omitted | |
if [ -z "$1" ]; then | |
read -p"Directory: " IDIR | |
else | |
IDIR=$1 | |
log Directory: $1 | |
fi | |
log | |
# Check if IDIR exists | |
if [ ! -d "$IDIR" ]; then | |
echo "$IDIR" is not a directory >&2 | |
exit 1 | |
fi | |
cd "$IDIR" || exit | |
# Find files to convert | |
FILES_COUNT=0 | |
for f in *.$IEXT; do | |
if [ -f "$f" ]; then | |
FILES[$FILES_COUNT]=${f%.*} | |
((FILES_COUNT=FILES_COUNT+1)) | |
fi | |
done | |
log Found $FILES_COUNT files | |
[ $FILES_COUNT -eq 0 ] && exit | |
if [ $NONO -eq 0 ]; then | |
mkdir -p $ODIR | |
else | |
log No action | |
fi | |
log | |
# Padding | |
FILES_COUNT_LEN=${#FILES_COUNT} | |
# Execute | |
for (( i=0; i<$FILES_COUNT; i++ )); do | |
f=${FILES[$i]} | |
printf -v C "%0${FILES_COUNT_LEN}g" $((i+1)) | |
log $C/$FILES_COUNT "$f.$IEXT" | |
[ $NONO -eq 0 ] && magick-cmd "$f" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment