Tuesday, November 19, 2013

Drag-and-drop batch image resizing in Windows using ImageMagick and ExifTool

I became frustrated when a friend of mine kept sending me image attachments via e-mail that were in the RAW format and at the high resolutions he originally took his shots with. While my connection could handle the large files, I figured there were probably some listed addresses he sent these same images to where the receiver would get frustrated at how long it would take to download. This friend isn't very tech savvy and I've found that Windows makes it difficult to batch resize images so I came up with a batch file in which he could place wherever (hopefully his Pictures folder) that he could drag any folder into it and it would resize all the images in the folder to his default liking.

These instructions operate under the following assumptions:
  1. ImageMagick is installed to C:\Program Files\ImageMagick
  2. A copy of ExifTool (exiftool.exe) has been placed in your system folder at %SystemRoot%\System32
This version (0.3) is optimal for converting many images, however, this script is extremely memory-intensive. So run this while you're not doing too many other things on your computer.
 @echo off  
   
 rem 0.3  
 setlocal enabledelayedexpansion  
 set ImageDir=%1  
 set ImageFileTypes=bmp, gif, jpeg, jpg, png  
 set ResizedImageDir="%ImageDir:"=% (Resized)"  
 set ResizedImageGeometry=640x480  
 set RemoveImageMetadata=1  
 if not exist %ImageDir%\* (  
      exit  
 )  
 if not exist %ResizedImageDir%\* (  
      md %ResizedImageDir%>nul 2>&1  
 )  
 for %%a in (%ImageFileTypes%) do (  
      set ImageCount=0  
      for /f "tokens=*" %%b in ('dir /a:-d /b "%ImageDir:"=%\*.%%a" 2^>nul') do (set /a ImageCount+=1)  
      if !ImageCount! gtr 0 (  
           echo Resizing %%a images...  
           rem convert "C:\Images\*.xxx[640x480] -set filename:original "%t.%e" "C:\Images (Resized)\%[filename:original]">nul 2>&1  
           convert "%ImageDir:"=%\*.%%a[%ResizedImageGeometry%]" -set filename:original "%%t.%%e" "%ResizedImageDir:"=%\%%[filename:original]">nul 2>&1  
             
           if %RemoveImageMetadata% equ 1 (  
                rem echo Stripping %%a images of all profiles and comments...  
                rem mogrify -strip "C:\Images (Resized)\*.jpg">nul 2>&1  
                rem mogrify -strip "%ResizedImageDir:"=%\*.%%a">nul 2>&1  
   
                echo Deleting all meta information from %%a images...  
                rem exiftool -overwrite_original -all= "C:\Images (Resized)\*.jpg">nul 2>&1  
                exiftool -overwrite_original -all= "%ResizedImageDir:"=%\*.%%a">nul 2>&1  
           )  
      )  
 )  
Save the file as "batchImageResizer.bat"

This version (0.2) is an alternate build that is less memory-intensive.
 @echo off  
   
 rem 0.2  
 setlocal enabledelayedexpansion  
 set ImageDir=%1  
 set ImageFileTypes=bmp, gif, jpeg, jpg, png  
 set ResizedImageDir="%ImageDir:"=% (Resized)"  
 set ResizedImageGeometry=640x480  
 set RemoveImageMetadata=1  
 if not exist %ImageDir%\* (  
      exit  
 )  
 if not exist %ResizedImageDir%\* (  
      md %ResizedImageDir%>nul 2>&1  
 )  
 for %%a in (%ImageFileTypes%) do (  
      set ImageCount=0  
      for /f "tokens=*" %%b in ('dir /a:-d /b "%ImageDir:"=%\*.%%a" 2^>nul') do (  
           set /a ImageCount+=1  
           if not exist "%ResizedImageDir:"=%\%%~nxb" (  
                echo Resizing %%~nxb...  
                rem convert "C:\Images\image.jpg" -resize 640x480 "C:\Images (Resized)\image.jpg">nul 2>&1  
                convert "%ImageDir:"=%\%%~nxb" -resize %ResizedImageGeometry% "%ResizedImageDir:"=%\%%~nxb">nul 2>&1  
           )  
      )  
      if %RemoveImageMetadata% equ 1 (  
           if !ImageCount! gtr 0 (  
                rem echo Stripping %%a images of all profiles and comments...  
                rem mogrify -strip "C:\Images (Resized)\*.jpg">nul 2>&1  
                rem mogrify -strip "%ResizedImageDir:"=%\*.%%a">nul 2>&1  
   
                echo Deleting all meta information from %%a images...  
                rem exiftool -overwrite_original -all= "C:\Images (Resized)\*.jpg">nul 2>&1  
                exiftool -overwrite_original -all= "%ResizedImageDir:"=%\*.%%a">nul 2>&1  
           )  
      )  
 )  
Save the file as "batchImageResizer.bat"

You can modify some of the variables to your own liking.  As-is, if you drop a folder of images into this batch file it will create a new folder in the same directory, resize all the images to 640x480, and scrub the EXIF data from the images.

Enjoy. :)

1 comment: