Click here to Skip to main content
13,247,005 members (80,024 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 28 Sep 2009

Resize images using the Windows Script Host

, 28 Sep 2009
Rate this:
Please Sign up or sign in to vote.
How to resize images without any special programs.

Resize Image Script usage


The Internet is full of all kinds of programs, both shareware and freeware, to resize images. Turns out, you don't need a program if you have access to a Windows PC with at least Windows Vista (XP from SP1 on is also supported, but requires a separate download).

Resizing can be done entirely in a script using Windows Image Acquisition Automation. Here is a script that will resize all the images in a given directory.


Anyone who has ever created a webpage with thumbnails may find this useful.

Using the Code


resize_image.wsf /indir:"My Pictures" /width:160 /height:120

All options can be found by running resize_image.wsf (or just look at the image above).

How it Works

Windows Image Acquisition (WIA) COM objects include WIA.ImageFile and WIA.ImageProcess. After an image file has been loaded with ImageFile.LoadFile(), various filters can be used on it via ImageProcess.Filters.Add(). One of the filters (called Scale) can scale the image. This is what the script is using:

var Img = WScript.CreateObject("WIA.ImageFile.1");
var IP = WScript.CreateObject("WIA.ImageProcess.1");
var fso = WScript.CreateObject("Scripting.FileSystemObject");
function resize(filename)
    var path = inDir + "\\" + filename;
    var newWidth, newHeight;


    // Initialize filters once.
    if (IP.Filters.Count == 0)

    // Find out if the image is Portrait orientation or Landscape.
    // Maintain its orientation.
    if (Img.Width > Img.Height)
        newWidth = scaledWidth;
        newHeight = scaledHeight;
    else    // Portrait => flip width with height
        newWidth = scaledHeight;
        newHeight = scaledWidth;

    IP.Filters(1).Properties("MaximumWidth") = newWidth;
    IP.Filters(1).Properties("MaximumHeight") = newHeight;

    if (Img.Width > newWidth || Img.Height > newHeight)
        Img = IP.Apply(Img);
        // apply filter (this will resize the image in memory)

        // Create subdirectory if needed.
        if (!fso.FolderExists(outDir))
        var newPath = outDir + "\\" + filename;
        if (!fso.FileExists(newPath))
        // don't overwrite existing files
            return true;

    return false;

Points of Interest

I found there was no version-independent ProgID (without ".1") on my XP SP3 machine. That's why WScript.CreateObject() calls use version 1 ProgIDs (e.g., WIA.ImageFile.1 instead of WIA.ImageFile). So far, there is only version 1 of the WIA automation library anyway. However, my Windows 7 machine contains version-independent WIA ProgIDs, so ".1" could be removed.

Another thing - if you run the script under the default WSH host (WScript), it will display a popup before going through the directory and another after it's done (see Echo commands). To run in non-blocking mode (text output instead of graphical popups), start it under cscript:

cscript resize_image.wsf


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Sergey Vasilevskiy
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 28 Sep 2009
Article Copyright 2009 by Sergey Vasilevskiy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid