How would I automate command line software that only takes processes one image at a time?

The software only takes one image in both the GUI and CLI version.

According to the command line help option for ImageResizer v129 (imgresizer.exe -h), you can specify multiple files to process at the command line:

You can load and process multiple files at once by loading after saving again.

ex. ImageResizer example code

imageresizer.exe /load 1.bmp /resize 10x10 Pixel /save 1.jpg /load 2.bmp /resize 10x10 Pixel /save 2.jpg

At a cursory glance, this option appears to be available in the standalone versions of ImageResizer since ImageResizer-r17.exe.


Also, from ImageResizer v121 (ImageResizer-r121.exe) onward, "scripts" appear to be supported e.g.:

ex. example_script.txt

/load 1.png /resize auto "LQ 4x" /save 1.png
/load 2.png /resize auto "LQ 4x" /save 2.png

ex. ImageResizer command

imgresizer.exe /script example_script.txt

So using this option could allow multiple files to be processed at once as well.


As with regular invalid commands, ImageResizer seems to print its "help" information if it cannot process the given script.


How would I go about automating [this]?

General Approaches

Since at least some versions of ImageResizer can apparently take batch instructions ("scripts", above), your basic choices would be to either:

  • Create a script to directly call imageresizer.exe repeatedly for each file.

  • Create a script (or use other methods) to make text files for ImageResizer to consume in order to process your desired images.

Simple Examples

  1. In the folder containing your files, create a text file containing only file and folder names with ex.:

     dir /b > filenames.txt
    
  2. Remove any folder names from e.g. filenames.txt. Repeat for any file names you do not wish to process, then save.


Option 3A - Traditional Script

Batch FOR loops can be used to repeatedly call an executable. Create a text file with a batch extension (.bat) and content similar to the following:

ex. resizer.bat

FOR /F %%G IN (filenames.txt) DO (
    imageresizer.exe /load %%G /resize auto "LQ 4x" /save %%G
)

Place e.g. filenames.txt and this batch file in the same directory as the files you wish to process. Then run ex. resizer.bat from the command line or just by double clicking it. In this case, %%G is the line read from ex. filenames.txt.


Option 3B - ImageResizer "Script"

Open e.g. filenames.txt in Notepad++, then use regular expressions to replace the simple file names with your desired command(s):

  • Open the Notepad++ Replace dialog with Ctrl + H.

  • Make certain that the Wrap around and Regular expression options are marked.

  • In the Find what: field, in parentheses, put .* then the file extension you wish to affect ex. .png:

    (.*.png)
    
  • In the Replace with field, put your ImageResizer command but with $1 in place of the file names ex.:

    /load $1 /resize auto "LQ 4x" /save $1
    

    %1 is replaced by the matches found with ex. (.*.png).

  • Select Replace all.

    ex. Notepad++ - Replace dialog

    enter image description here

  • This should turn all your file names into ex.:

    /load file1.png /resize auto "LQ 4x" /save file1.png
    /load file2.png /resize auto "LQ 4x" /save file2.png 
    
  • Save your new text file (e.g. as example_script.txt), then use imageresizer.exe to run the script ex.:

    imgresizer.exe /script example_script.txt
    

Python

An example of calling imgresizer.exe /load [IMAGE.png] /resize auto "LQ 4x" /save [OUTIMAGE].png for each .png image in the current directory and all sub-directories via Python 3 on Windows:

# An example of how to use os.walk() and subprocess.run() to find desired files
# and feed them to ImageResizer.

import os
import os.path
import subprocess


# --- Variables, Etc. ---

# Directory where our files are stored. '.' is the current directory (whichever
# directory this script appears in). However, this can be any starting folder.
ROOT_DIR = '.'

# What type of files are we looking for?
# PREFIX = 'image_'
EXT = '.png'

# A list to hold our file path information.
full_paths = []


# --- Functions ---

# A small, custom function to build our ImageResizer command.
def build_command(filepath):

    # This string is directly invoked at the command line. Watch for spacing.
    # "\" breaks our long command into two separate lines.
    cmd_str = 'imageresizer /load ' + filepath + \
              ' /resize auto "LQ 4x" /save ' + filepath + '.jpg'

    return cmd_str


# ----- Main -----

for dirpath, dirnames, filenames in os.walk(ROOT_DIR):

    # Track the full path to our individual files.
    for name in filenames:

        # if name.startswith(PREFIX) and name.endswith(EXT):
        if name.endswith(EXT):

            # Test code
            # print(name)

            full_path = os.path.join(dirpath, name)
            full_paths.append(full_path)

# -----

# Visual aid
print('')

for path_item in full_paths:

    # Test code
    # print(path_item)

    # Put our file paths in quotes so we don't get errors when processing
    # sub-directories with spaces in their names.
    path_item = '"' + path_item + '"'

    try:

        # Custom function -> build_command()
        cmd = build_command(path_item)

        # Test code
        # print(cmd)

        subprocess.run(cmd, check=True)

    # Catch/print errors produced when calling ImageResizer with subprocess.
    except (OSError, subprocess.CalledProcessError) as err:

        # pass

        print('')
        print(err)

Python Script Notes

  • os.walk() reads file/directory information and subprocess.run() is used to call external, non-Python commands, etc. i.e imageresizer.exe.

  • If using a full path for ROOT_DIR, use \\ rather than just\ for path separators ex.:

    ROOT_DIR = 'C:\\Example\\Path'
    
  • os.walk(ROOT_DIR) yields three items for each directory in a tree:

    • dirpath is the path to the current (sub-)directory.

    • dirnames is a list of the names of the subdirectories in dirpath (excluding . and ..).

    • filenames is a list of the names of the non-directory files in dirpath.

  • Names in the dirnames and filenames lists above are "bare" i.e. they do not contain any path components.

  • OSError catches errors produced by the OS i.e. with files etc. and subprocess.CalledProcessError lets us know if there was an issue with the process called by subprocess.run() (i.e. it returned a non-zero value).


Python Import References

os

os.path

subprocess


The above answer is correct, however I want to add my two cents.

The process you want can be done with PowerShell, if you don't know how to open it, you can use:

Win+R>>type powershell>>Ctrl+Shift+Enter

Then you can use these codes to recursively process all images:

$files=(Get-ChildItem -path "path\to\folder" -force -recurse -filter "*.png").fullname | %{if ($_ -match "\s") {'"'+$_+'"'}}
foreach ($file in $files) {Invoke-Command $("imgresizer.exe /load {0} /resize auto "LQ 4x" /save {0}" -f $file)}

Just replace "path\to\folder" with the actual path.

P.S. The above command will only work if 1, the full path of the executable imageresizer.exe is supplied; 2, you changed directory to the path where the executable is located; And finally, 3, you don't need to cd to the path of the .exe or use its full path if you have added its path to the Path environment variable.