20

I'm looking for some help in writing a batch script to resize a bunch of .jpg images.

I don't have much experience with batch scripts. But this task will be preformed on a windows machine & so I thought a batch script might be a good way to go.

I'm always interested in hearing alternative ideas & approaches, or being made aware of elements I haven't thought of.

Below I have listed the basic steps/needs of the script:

1) The images are located in a folder & are all(or should be) 500 x
500.

2) I need copy & past the images to a new folder, where they will be
resized to 250 x 250.

3) I then need to repeat step 2 but this time resize to 125 x 125.
Reed Williams
  • 277
  • 1
  • 4
  • 10

4 Answers4

24

Once you install ImageMagick for Windows, you can use magick command-line tool, e.g.

magick.exe mogrify -resize 250x250 -path 250x250/ *.png *.jpg
magick.exe mogrify -resize 125x125 -path 125x125/ *.png *.jpg

Note: Make sure your magick.exe command is in your PATH system variable and you're pointing to the existing or created the destined folders (e.g. mkdir 250x250/ 125x125/ in above case).

For Linux/Ubuntu, see: How to easily resize images via command-line?

kenorb
  • 118,428
  • 63
  • 588
  • 624
  • 2
    Just make sure the folder exists before you run it. – Jeff Sep 23 '17 at 17:14
  • Before running this, run `mkdir 250x250` and `mkdir 125x125` – Andrei Krasutski Oct 11 '17 at 09:28
  • Upvoting this, since the question specifically asked for doing it from a batch script - ImageMagick would allow this, and is a tried and tested, popular tool for automating image operations. – Rohaq Nov 16 '18 at 02:51
23

Use Image Resizer for Windows:

enter image description here

enter image description here

  • 2
    For posterity Image Resizer is now part of the resurrected Microsoft PowerToys https://github.com/microsoft/PowerToys – Caltor Feb 15 '21 at 11:57
15

you can check scale.bat which can resize images without a need of installing additional software - it uses only the windows built-in capabilities:

@echo off
set "source_folder=c:\images"
set "result_folder_1=c:\res1"
set "result_folder_2=c:\res2"

for %%a in ("%source_folder%\*jpg") do (
   call scale.bat -source "%%~fa" -target "%result_folder_1%\%%~nxa" -max-height 250 -max-width 250 -keep-ratio no -force yes
)

for %%a in ("%source_folder%\*jpg") do (
   call scale.bat -source "%%~fa" -target "%result_folder_2%\%%~nxa" -max-height 125 -max-width 125 -keep-ratio no -force yes
)

Check also this.

npocmaka
  • 51,748
  • 17
  • 123
  • 166
  • 5
    I like this solution best. It is simple, uses what is already on the system and also provides good example on how to interface with the system to extend or do something else. Good Job! – Tiris Nov 01 '17 at 15:48
  • I'm sorry to say, but the quality of resized PNG files is low - it seems it uses the simplest "nearest neighbor" algorithm. – Markus von Broady Jun 05 '20 at 12:10
  • @MarkusvonBroady - thanks for the note. Though with automating the existing windows tools I can't do more. – npocmaka Jun 05 '20 at 13:20
6

If you want to do it on the command line specifically, perhaps so you can automate it, there is no specific command in Batch that is made for image manipulation. You can code up something in a JScript or another language and run it from the command line, but why do that when there are mature tools already available for this task?

I recommend ImageMagick.

Get the portable Windows binary, then you can use magick.exe to do what you want pretty easily. For example, to resize (by half) all the png images in folder 1 to folder 2:

@echo off
if not exist 2 md 2
for %%a in (1\*.png) do "path\to\magick.exe" -resize 50x50% "1\%~nxa" "2\%~nxa"
Glorfindel
  • 19,729
  • 13
  • 67
  • 91
soja
  • 1,407
  • 6
  • 8
  • 2
    Since this answer was posted, ImageMagick's exe is now called `magick.exe` not `convert.exe`. – Tahir Hassan Mar 19 '17 at 18:20
  • Changed convert to magick – soja Mar 21 '17 at 02:31
  • Since you use ImageMagick, it may be wiser to use the correct resize procedure... https://www.imagemagick.org/Usage/resize/#resize_colorspace that means using: for %%a in (1\*.png) do "path\to\magick.exe "1\%~nxa" -colorspace RGB -resize 50x50% -colorspace sRGB "2\%~nxa" – FarO Dec 11 '17 at 22:33
  • My comment is valid for downscaling. If you upscale images, stick with the commendline of the answer. – FarO Dec 11 '17 at 22:39
  • "but why do that when there are mature tools already available for this task" - because you may not want to make a tool that weighs an additional 77 MB due to ImageMagick. In case of JPG files, there is a simple tool http://www.rw-designer.com/picture-resize . – Markus von Broady Jun 05 '20 at 12:14