batch resize in a folder?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
suhanto

batch resize in a folder?

Post by suhanto »

How do I perform batch-resizing for several images in a folder?

for example I have four images in folder C:\temp\

image1.png
image2.png
image3.png
image4.png

and I want to convert them to JPG and resize them to 60% and lower the quality of each image to 60%.

Can I do this using batch file (*.bat) in Windows?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: batch resize in a folder?

Post by fmw42 »

cd to the directory where your images are located (temp) after creating a new directory to hold the changed files (say, temp2)

mogrify -path fullpathto/temp2 -resize 60x60% -quality 60 -format jpg *.png

this will take all png files in your current directory (temp), resize to 60% (of largest dimension and keep aspect ratio), set jpg quality to 60 and convert to jpg.

see

http://www.imagemagick.org/Usage/basics/#mogrify


windows user should read the following in general for differences with unix

http://www.imagemagick.org/Usage/windows/
suhanto

Re: batch resize in a folder?

Post by suhanto »

Thanks, now I've got what I'm looking for. Your tips really do the task!
Andrey_Ra

Re: batch resize in a folder?

Post by Andrey_Ra »

I have a nice code for "Send To" batch. You need to save this code as "Resize.bat" file and place it in "SendTo" folder for a current user.
This code automatically creates subfolder named as destination size ("700x700"), resizes all JPG-files which were found in a current folder, places all resized files in "700x700"-folder and deletes original files if they were resized correctly.

My wife often use this code to resize images for a livejournal. Just copy all images in separate folder, right-click on any file, Send it To "Resize.bat" and "voila" - you got perfectly resized images with a nice borders!

I have tested it on Windows Server 2003, Vista and Windows 7 x64.
  1. You can set size as you like (default is 700x700).
  • You must set full path to "convert.exe" in this batch-file.

Code: Select all

@ECHO OFF

set resizeSize=700x700
set dirCnt=0
set tmpCnt=0
set convert_exe=C:\Program Files\ImageMagick-6.6.2-Q16\convert.exe
(set foldername=%1)

IF NOT EXIST "%convert_exe%" (
  echo ImageMagick path is incorrect. You need to set it up in batch file!
  echo Current value: %convert_exe%
  pause
  exit 4
 )

for /F "usebackq delims=" %%i in (`dir /a:-d /b %foldername%\*.jpg`) do set /a dirCnt+=1

IF (%dirCnt%) EQU (0) (
  call set foldername=%%~dp1
  for /F "usebackq delims=" %%i in (`dir /a:-d /b %%foldername%%\*.jpg`) do set /a dirCnt+=1
 )

IF (%dirCnt%) EQU (0) (
  echo JPG files not found in folder "%foldername%". Exiting.
  pause
  EXIT 1
 )

echo Found %dirCnt% JPG files in folder %foldername%.

FOR /F "usebackq delims=" %%i in (`dir /B /A:-D %foldername%`) do (
  IF %%i EQU %%resizeSize (
    echo Found file with name "%resizeSize%". You need to remove this file from folder.
    PAUSE
    exit 3
   )
 )

set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /B /A:D %foldername%`) do (
  if %%i EQU %resizeSize% set /a tmpCnt+=1
 )

IF (%tmpCnt%) EQU (0) md %foldername%\%resizeSize%

set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /B /A:D %foldername%`) do (
  if %%i EQU %resizeSize% set /a tmpCnt+=1
 )

IF (%tmpCnt%) EQU (0) (
  echo Error! Can't create folder "%foldername%\%resizeSize%".
  pause
  EXIT 2
 )

echo Resizing JPG files in "%foldername%":
 
set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /a:-d /b %foldername%\*.jpg`) do (
  set /a tmpCnt+=1
  call echo Proceed file %%tmpCnt%% from %%dirCnt%%: %%i.
  call cmd /c "TYPE "%%foldername%%\%%i" | "%convert_exe%" jpg:fd:0 -filter lagrange -set option:filter:support 2.0 -set option:filter:win-support 12 -set option:filter:lobes 8 -resize %%resizeSize%% -bordercolor white -border 8 -bordercolor grey60 -border 1 "%%foldername%%\%%resizeSize%%\%%i""

  IF EXIST "%foldername%\%resizeSize%\%%i" (
    echo  Success.
    echo  Deleting source file: '%foldername%\%%i'.
    del /q "%foldername%\%%i"
   ) ELSE (
    echo Unknown error is occurred! Aborting.
    pause
    exit 5
   )
 )

echo ""
echo Resizing is done, check folder %resizeSize% for results!
echo ""
pause
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: batch resize in a folder?

Post by Bonzo »

Thats some interesting code Andrey - specialy seeing some error reporting. The batch scripts I have written do not have any and if anything goes wrong I spend ages searching for the problem !

I thought I would give it a go but can not find a "send to" folder on my Vista system. When I get a moment I will have another look for it.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: batch resize in a folder?

Post by Bonzo »

I found the send to folder in the end and have tried the code.
A couple of comments:
1/ I did not like deleting the original images and so I comented out

Code: Select all

IF EXIST "%foldername%\%resizeSize%\%%i" (
    echo  Success.
    ::echo  Deleting source file: '%foldername%\%%i'.
    ::del /q "%foldername%\%%i"
2/ I recived an error - not sure if it was there before commenting out the delete

Code: Select all

Found 6 JPG files in folder C:\Users\Anthony\Desktop\TEST.
Resizing JPG files in "C:\Users\Anthony\Desktop\TEST":
Proceed file 1 from 6: IMG_0002.JPG.
 Success.
The system cannot find the drive specified.
Proceed file 2 from 6: IMG_0003.JPG.
 Success.
The system cannot find the drive specified.
Proceed file 3 from 6: IMG_0204.JPG.
 Success.
The system cannot find the drive specified.
Proceed file 4 from 6: IMG_1455.JPG.
 Success.
The system cannot find the drive specified.
Proceed file 5 from 6: IMG_1456.JPG.
 Success.
The system cannot find the drive specified.
Proceed file 6 from 6: IMG_1473.JPG.
 Success.
The system cannot find the drive specified.
""
Resizing is done, check folder 700x700 for results!
""
Press any key to continue . . .
3/ Every time I upgrade IM I will need to remember to modify this batch file.
jaffamuffin
Posts: 59
Joined: 2009-01-30T03:46:08-07:00

Re: batch resize in a folder?

Post by jaffamuffin »

change your comment tag from :: to REM

there are special cases where :: doesn't work (inside a for loop may be one) it's possible windows is interpreting the letter after the : as a drive letter hence the error message.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: batch resize in a folder?

Post by Bonzo »

Thanks for the tip - changing :: to REM worked.
c57d
Posts: 1
Joined: 2010-11-28T19:15:18-07:00
Authentication code: 8675308

Re: batch resize in a folder?

Post by c57d »

The code works great except when there is a space in the folder name.
On a 64 bit Win7 machine with a folder called c:\test I get this as the result (echo on, some items removed for brevity):
**(set foldername=C:\test\PP0306_DigiTool_image1.jpg )
**for /F "usebackq delims=" %i in (`dir /a:-d /b C:\test\PP0306_DigiTool_image1.jpg\*.jpg`) do set /a dirCnt+=1
File Not Found

**IF (0) EQU (0) (call set foldername=%~dp1 for /F "usebackq delims=" %i in (`dir /a:-d /b %foldername%\*.jpg`) do set /a dirCnt+=1)
**set /a dirCnt+=1

**IF (1) EQU (0) (
echo JPG files not found in folder "C:\test\". Exiting.
pause
EXIT 1
)
**echo Found 1 JPG files in folder C:\test\.
Found 1 JPG files in folder C:\test\.

But when I rename the folder to test 1 I get:
**(set foldername="C:\test 1\PP0306_DigiTool_image1.jpg" )
**for /F "usebackq delims=" %i in (`dir /a:-d /b "C:\test 1\PP0306_DigiTool_image1.jpg"\*.jpg`) do set /a dirCnt+=1
File Not Found

**IF (0) EQU (0) (call set foldername=%~dp1 for /F "usebackq delims=" %i in (`dir /a:-d /b %foldername%\*.jpg`) do set /a dirCnt+=1)
The system cannot find the file specified.

**IF (0) EQU (0) (
echo JPG files not found in folder "C:\test 1\". Exiting.
pause
EXIT 1
)
JPG files not found in folder "C:\test 1\". Exiting.
Press any key to continue . . .


I can see that it is quoting the path name "C:\test 1\" but still not finding the files if a space is in the path.

Any suggestions on how to correct this without having to take the spaces out of my folder names?

Thank you,

Chris
stevemaddensf
Posts: 1
Joined: 2012-12-04T08:31:58-07:00
Authentication code: 6789

Re: batch resize in a folder?

Post by stevemaddensf »

I 'd like first to thank everyone who posted before on different places, I can't remind all the pages I visited, (including this one).
I faced the same problem, and had to fix this script for my own use in XP, including
1) the name spacing problem for directory and picture name.
2) removing the double-quote inside the variable when gluing 2 strings
3) delayed evaluation of variables
This script works by adding it inside "sendto" directory menu->run-> type 'sendto' , (for some reason, calling it inside cmd generate error, but dont care, just right click a directory, sendto-> resize_600.bat it works !)

resize_600.bat

Code: Select all

@ECHO OFF

set resizeSize=600x600
set dirCnt=0
set tmpCnt=0
set convert_exe=C:\Program Files\ImageMagick-6.8.0-Q16\convert.exe
set foldername=%1
REM remove double quote from string
set distfolder=%foldername%"\web600"
set distfolder=%distfolder:""=%



REM =(1)============< CHECK PATH MAGIC >============================================
IF NOT EXIST "%convert_exe%" (
  echo ImageMagick path is incorrect. You need to set it up in batch file!
  echo Current value: %convert_exe%
  pause
  exit 4
 )

REM =(2)============< COUNT JPG files >===================================================
for /F "usebackq delims=" %%i in (`dir /a:-d /b %foldername%\*.jpg`) do set /a dirCnt+=1

IF (%dirCnt%) EQU (0) (
  call set foldername=%%~dp1
  for /F "usebackq delims=" %%i in (`dir /a:-d /b %%foldername%%\*.jpg`) do set /a dirCnt+=1
 )
IF (%dirCnt%) EQU (0) (
  echo JPG files not found in folder "%foldername%". Exiting.
  pause
  EXIT 1
 )
echo Found %dirCnt% JPG files in folder %foldername%.



REM =(3)===============< MAKE WEB DIR >===============================
set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /B /A:D %foldername%`) do (
 if %%i EQU web600 set /a tmpCnt+=1
 )
IF (%tmpCnt%) EQU (0) md %foldername%\web600
REM ==============================================================
set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /B /A:D %foldername%`) do (
  if %%i EQU web600 set /a tmpCnt+=1
 )
IF (%tmpCnt%) EQU (0) (
  echo Error! Can't create folder "%foldername%\web600".
  pause
  EXIT 2
 )
REM ==============================================================



echo Resizing JPG files in %foldername%:

chdir /D %foldername%
set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /a:-d /b %foldername%\*.jpg`) do (
  set /a tmpCnt+=1
  call echo Proceed file %%tmpCnt%% from %%dirCnt%%: "%%i"

"%convert_exe%" "%%i" -interpolate bicubic -filter lagrange -set option:filter:support 2.0 -set option:filter:win-support 12 -set option:filter:lobes 8  -resize %resizeSize%  -quality 95 ".\web600\%%i"
 )

echo ""
echo Resizing is done, check folder web for results!
echo ""
pause

For bonus next script add a white band on right for Horizontal pix and a black 1/2 band on top and bottom of Vertical pix when converting 4:3 picture to a 10cm x 15cm format. My local store only provide 10x15 cm print and they cut the picture when provided in 4:3, so stupid !
White band is to be removed after printing, black one is to stay because if you cut a vertical picture, then it will fell from albums (10x15 ones) because they would be loose.

convert_10x15.bat

Code: Select all

@ECHO OFF
setlocal EnableDelayedExpansion
set dirCnt=0
set tmpCnt=0
set convert_exe=C:\Program Files\ImageMagick-6.8.0-Q16\convert.exe
set foldername=%1
REM remove double quote from string
set distfolder=%foldername%"\print"
set distfolder=%distfolder:""=%


REM =(1)============< CHECK PATH MAGIC >============================================
IF NOT EXIST "%convert_exe%" (
  echo ImageMagick path is incorrect. You need to set it up in batch file!
  echo Current value: %convert_exe%
  pause
  exit 4
 )

REM =(2)============< COUNT JPG files >===================================================
for /F "usebackq delims=" %%i in (`dir /a:-d /b %foldername%\*.jpg`) do set /a dirCnt+=1

IF (%dirCnt%) EQU (0) (
  call set foldername=%%~dp1
  for /F "usebackq delims=" %%i in (`dir /a:-d /b %%foldername%%\*.jpg`) do set /a dirCnt+=1
 )
IF (%dirCnt%) EQU (0) (
  echo JPG files not found in folder "%foldername%". Exiting.
  pause
  EXIT 1
 )
echo Found %dirCnt% JPG files in folder %foldername%.



REM =(3)===============< MAKE WEB DIR >===============================
REM if not exist "%1\print\nul" md "%1\print"

set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /B /A:D %foldername%`) do (
 if %%i EQU print set /a tmpCnt+=1
 )
IF (%tmpCnt%) EQU (0) md %foldername%\print
REM ==============================================================
set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /B /A:D %foldername%`) do (
  if %%i EQU print set /a tmpCnt+=1
 )
IF (%tmpCnt%) EQU (0) (
  echo Error! Can't create folder "%foldername%\print".
  pause
  EXIT 2
 )
REM ==============================================================

echo Adding a marge to pictures inside  %foldername%:

chdir /D %foldername%
echo CHEMIN %cd%
set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /a:-d /b %foldername%\*.jpg`) do (
  set /a tmpCnt+=1

echo FILE [%%i]

  FOR /F %%x IN ('identify -ping -format "%%w" "%%i"') DO SET Iwidth=%%x
  FOR /F %%x IN ('identify -ping -format "%%h" "%%i"') DO SET Iheight=%%x

  call echo Proceed file %%tmpCnt%% from %%dirCnt%%: "%%i" size %%Iwidth%% x %%Iheight%%

if !Iwidth! GEQ !Iheight! (

echo HORIZONTAL
set /a ratio=15*!Iheight!
set /a ratio=!ratio!/10
   if !ratio! GEQ !Iwidth! (
     set /a pix2add=!ratio!-!Iwidth!
   ) ELSE (set a/ pix2add=0 )

::echo RATIO !pix2add!
   "%convert_exe%" "%%i" -gravity East -background white -splice !pix2add!x0  -quality 95 ".\print\%%i"
) ELSE (

echo VERTICAL

set /a ratio=15*!Iwidth!
set /a ratio=!ratio!/10
   if !ratio! GEQ !Iheight! (
     set /a pix2add=!ratio!-!Iheight!
     set /a pix2add=!pix2add!/2
   ) ELSE (set a/ pix2add=0 )


"%convert_exe%" "%%i" -bordercolor black -border 0x!pix2add!  -quality 95 ".\print\%%i"

 )
) 
echo "-----------------------------------------" 
echo Marge added, in rep .\print 
echo "-----------------------------------------"
pause

Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: batch resize in a folder?

Post by Bonzo »

Thanks for the code stevemaddensf; it is always useful having batch file examples.
propman07
Posts: 2
Joined: 2018-10-08T10:08:08-07:00
Authentication code: 1152

Re: batch resize in a folder?

Post by propman07 »

All-

I know that this is an old thread....but I wanted to post my findings....

I tried to implement the code provided by stevemaddensf (thanks), but still get an error when trying to process images that are in a folder with spaces in the name.

Any help would be appreciated....off to continue to google for a solution.

Thanks.
stevemaddensf wrote: 2012-12-04T08:49:58-07:00 I 'd like first to thank everyone who posted before on different places, I can't remind all the pages I visited, (including this one).
I faced the same problem, and had to fix this script for my own use in XP, including
1) the name spacing problem for directory and picture name.
2) removing the double-quote inside the variable when gluing 2 strings
3) delayed evaluation of variables
This script works by adding it inside "sendto" directory menu->run-> type 'sendto' , (for some reason, calling it inside cmd generate error, but dont care, just right click a directory, sendto-> resize_600.bat it works !)

resize_600.bat

Code: Select all

@ECHO OFF

set resizeSize=600x600
set dirCnt=0
set tmpCnt=0
set convert_exe=C:\Program Files\ImageMagick-6.8.0-Q16\convert.exe
set foldername=%1
REM remove double quote from string
set distfolder=%foldername%"\web600"
set distfolder=%distfolder:""=%



REM =(1)============< CHECK PATH MAGIC >============================================
IF NOT EXIST "%convert_exe%" (
  echo ImageMagick path is incorrect. You need to set it up in batch file!
  echo Current value: %convert_exe%
  pause
  exit 4
 )

REM =(2)============< COUNT JPG files >===================================================
for /F "usebackq delims=" %%i in (`dir /a:-d /b %foldername%\*.jpg`) do set /a dirCnt+=1

IF (%dirCnt%) EQU (0) (
  call set foldername=%%~dp1
  for /F "usebackq delims=" %%i in (`dir /a:-d /b %%foldername%%\*.jpg`) do set /a dirCnt+=1
 )
IF (%dirCnt%) EQU (0) (
  echo JPG files not found in folder "%foldername%". Exiting.
  pause
  EXIT 1
 )
echo Found %dirCnt% JPG files in folder %foldername%.



REM =(3)===============< MAKE WEB DIR >===============================
set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /B /A:D %foldername%`) do (
 if %%i EQU web600 set /a tmpCnt+=1
 )
IF (%tmpCnt%) EQU (0) md %foldername%\web600
REM ==============================================================
set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /B /A:D %foldername%`) do (
  if %%i EQU web600 set /a tmpCnt+=1
 )
IF (%tmpCnt%) EQU (0) (
  echo Error! Can't create folder "%foldername%\web600".
  pause
  EXIT 2
 )
REM ==============================================================



echo Resizing JPG files in %foldername%:

chdir /D %foldername%
set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /a:-d /b %foldername%\*.jpg`) do (
  set /a tmpCnt+=1
  call echo Proceed file %%tmpCnt%% from %%dirCnt%%: "%%i"

"%convert_exe%" "%%i" -interpolate bicubic -filter lagrange -set option:filter:support 2.0 -set option:filter:win-support 12 -set option:filter:lobes 8  -resize %resizeSize%  -quality 95 ".\web600\%%i"
 )

echo ""
echo Resizing is done, check folder web for results!
echo ""
pause

For bonus next script add a white band on right for Horizontal pix and a black 1/2 band on top and bottom of Vertical pix when converting 4:3 picture to a 10cm x 15cm format. My local store only provide 10x15 cm print and they cut the picture when provided in 4:3, so stupid !
White band is to be removed after printing, black one is to stay because if you cut a vertical picture, then it will fell from albums (10x15 ones) because they would be loose.

convert_10x15.bat

Code: Select all

@ECHO OFF
setlocal EnableDelayedExpansion
set dirCnt=0
set tmpCnt=0
set convert_exe=C:\Program Files\ImageMagick-6.8.0-Q16\convert.exe
set foldername=%1
REM remove double quote from string
set distfolder=%foldername%"\print"
set distfolder=%distfolder:""=%


REM =(1)============< CHECK PATH MAGIC >============================================
IF NOT EXIST "%convert_exe%" (
  echo ImageMagick path is incorrect. You need to set it up in batch file!
  echo Current value: %convert_exe%
  pause
  exit 4
 )

REM =(2)============< COUNT JPG files >===================================================
for /F "usebackq delims=" %%i in (`dir /a:-d /b %foldername%\*.jpg`) do set /a dirCnt+=1

IF (%dirCnt%) EQU (0) (
  call set foldername=%%~dp1
  for /F "usebackq delims=" %%i in (`dir /a:-d /b %%foldername%%\*.jpg`) do set /a dirCnt+=1
 )
IF (%dirCnt%) EQU (0) (
  echo JPG files not found in folder "%foldername%". Exiting.
  pause
  EXIT 1
 )
echo Found %dirCnt% JPG files in folder %foldername%.



REM =(3)===============< MAKE WEB DIR >===============================
REM if not exist "%1\print\nul" md "%1\print"

set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /B /A:D %foldername%`) do (
 if %%i EQU print set /a tmpCnt+=1
 )
IF (%tmpCnt%) EQU (0) md %foldername%\print
REM ==============================================================
set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /B /A:D %foldername%`) do (
  if %%i EQU print set /a tmpCnt+=1
 )
IF (%tmpCnt%) EQU (0) (
  echo Error! Can't create folder "%foldername%\print".
  pause
  EXIT 2
 )
REM ==============================================================

echo Adding a marge to pictures inside  %foldername%:

chdir /D %foldername%
echo CHEMIN %cd%
set tmpCnt=0
for /F "usebackq delims=" %%i in (`dir /a:-d /b %foldername%\*.jpg`) do (
  set /a tmpCnt+=1

echo FILE [%%i]

  FOR /F %%x IN ('identify -ping -format "%%w" "%%i"') DO SET Iwidth=%%x
  FOR /F %%x IN ('identify -ping -format "%%h" "%%i"') DO SET Iheight=%%x

  call echo Proceed file %%tmpCnt%% from %%dirCnt%%: "%%i" size %%Iwidth%% x %%Iheight%%

if !Iwidth! GEQ !Iheight! (

echo HORIZONTAL
set /a ratio=15*!Iheight!
set /a ratio=!ratio!/10
   if !ratio! GEQ !Iwidth! (
     set /a pix2add=!ratio!-!Iwidth!
   ) ELSE (set a/ pix2add=0 )

::echo RATIO !pix2add!
   "%convert_exe%" "%%i" -gravity East -background white -splice !pix2add!x0  -quality 95 ".\print\%%i"
) ELSE (

echo VERTICAL

set /a ratio=15*!Iwidth!
set /a ratio=!ratio!/10
   if !ratio! GEQ !Iheight! (
     set /a pix2add=!ratio!-!Iheight!
     set /a pix2add=!pix2add!/2
   ) ELSE (set a/ pix2add=0 )


"%convert_exe%" "%%i" -bordercolor black -border 0x!pix2add!  -quality 95 ".\print\%%i"

 )
) 
echo "-----------------------------------------" 
echo Marge added, in rep .\print 
echo "-----------------------------------------"
pause

snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: batch resize in a folder?

Post by snibgo »

propman07 wrote:... but still get an error when trying to process images that are in a folder with spaces in the name.
The obvious solution: don't allow spaces in directory or file names.

If you really must, they generally need quoting.
snibgo's IM pages: im.snibgo.com
propman07
Posts: 2
Joined: 2018-10-08T10:08:08-07:00
Authentication code: 1152

Re: batch resize in a folder?

Post by propman07 »

Thanks for the reply....found my error...file works as advertised. Thanks.
Post Reply