137} I need to resize a group of images from the command line.
To do this you need a good image processing program like
Irfan Skiljan's IrfanView.
@echo off & setlocal enableextensions
::
echo +----------------------------------------------------------+
echo : IR_SCALE.CMD Resize a set of .jpg images to 80x60 pixels :
echo : By Prof. Timo Salmi, Last modified Mon 11-Aug-2008 :
echo +----------------------------------------------------------+
echo.
::
:: Set the defaults
set ir="C:\Program Files\IrfanView\i_view32.exe"
set sourceDir=C:\_M
set targetDir=C:\_M\SCALED
set pixW=80
set pixH=60
::
:: Is help needed
if "%~1"=="?" goto _help
if "%~1"=="/?" goto _help
if /i "%~1"=="/help" goto _help
::
:: If necessary, create the target folder
if not exist %targetDir% mkdir %targetDir%
::
:: Convert with IrfanView command line
:: See IrfanView Help "Command Line Options" for the switches
for %%f in (%sourceDir%\*.JPG) do (
start "" /wait %ir% %%~ff /resize_long=%PixW% /aspectratio /resample /jpgq=75 /convert=%targetDir%\%%~nf_scaled%%~xf
)
::
:: Show the results
dir %targetDir%
goto _out
::
:_help
echo Usage: IR_SCALE
echo.
echo Default source folder: %sourceDir%
echo Default target folder: %targetDir%
::
:_out
endlocal & goto :EOF
Another option is the NConvert command line program. The advantage of
NConvert is that it does not require a Windows installation. It is a
ready to run utility in the CLI. The disadvantage is that unlike in
IrfanView /resize_long we need to know in advance whether the image is
vertical or horizontal.
@echo off & setlocal enableextensions
set nconvert=C:\_F\BOX\BOXTOOLS\nconvert.exe
set filelist=C:\_M\TEMP\filelist.txt
if not exist C:\_M\SCALED\ mkdir C:\_M\SCALED
::
> "%filelist%" echo C:\_M\68080701.JPG
>>"%filelist%" echo C:\_M\68080702.JPG
>>"%filelist%" echo C:\_M\68080703.JPG
::
"%nconvert%" -keepfiledate -ratio -resize 60 0 -o C:\_M\SCALED\68073003_scaled.JPG C:\_M\68073003.JPG
"%nconvert%" -keepfiledate -ratio -resize 80 0 -o C:\_M\SCALED\%%_scaled.JPG -l "%filelist%"
::
for %%f in ("%filelist%") do if exist %%f del %%f
endlocal & goto :EOF
The resulting thumbnails:
IrfanView can be used to get an image's
dimensions into environment variables:
@echo off & setlocal enableextensions
set ir=C:\Program Files\IrfanView\i_view32.exe
set tmpfile_=C:\_M\irfan.tmp
set image_=C:\_M\68073003.JPG
::
"%ir%" "%image_%" /info="%tmpfile_%"
for /f "tokens=4,6" %%a in ('
type "%tmpfile_%"^|find "Image dimensions"') do (
set width_=%%a
set height_=%%b)
for %%f in ("%tmpfile_%") do if exist %%f del %%f
echo "%image_%"
echo width_=%width_% height_=%height_%
endlocal & goto :EOF
The output would be e.g.
C:\_M>c:\_d\test\cmdfaq
"C:\_M\68073003.JPG"
width_=768 height_=1024
Also NCovert can be used to get an image's dimensions:
@echo off & setlocal enableextensions
set nconvert=C:\_F\BOX\BOXTOOLS\nconvert.exe
set tmpfile_=C:\_M\nconvert.tmp
set image_=C:\_M\68073003.JPG
::
"%nconvert%" -info "%image_%"|find "Width ">"%tmpfile_%"
for /f "tokens=2 delims=:" %%a in ('
type "%tmpfile_%"') do set width_=%%a
"%nconvert%" -info "%image_%"|find "Height ">"%tmpfile_%"
for /f "tokens=2 delims=:" %%a in ('
type "%tmpfile_%"') do set height_=%%a
for %%f in ("%tmpfile_%") do if exist %%f del %%f
set width_=%width_: =%
set height_=%height_: =%
echo "%image_%"
echo width_=%width_% height_=%height_%
endlocal & goto :EOF
The output would be e.g.
C:\_M>c:\_d\test\cmdfaq
"C:\_M\68073003.JPG"
width_=768 height_=1024