Search code examples
batch-filecmdimage-file

Get image file dimensions in .bat file


I have a bat file that lists the paths of all images in a folder the code is

@echo off
break > infofile.txt
for /f "delims=" %%F in ('dir /b /s *.bmp') do (
   echo %%F 1 1 1 100 100 >>infofile.txt 
)


The text file looks like this

C:\Users\Charles\Dropbox\trainer\temp\positive\rawdata\diags(1).bmp 1 1 1 100 100  
C:\Users\Charles\Dropbox\trainer\temp\positive\rawdata\diags(348).bmp 1 1 1 100 100  
C:\Users\Charles\Dropbox\trainer\temp\positive\rawdata\diags(353).bmp 1 1 1 100 100  

What I want to do is replace the 100 100 at the end by the dimentions of each image width and height.. Thanks in advance.


Solution

  • you can use MediaInfo:

    @ECHO OFF &SETLOCAL
    (for /r %%a in (*.jpg *.bmp *.png) do (
        set "width="
        set "height="
        for /f "tokens=1*delims=:" %%b in ('"MEDIAINFO --INFORM=Image;%%Width%%:%%Height%% "%%~a""') do (
            echo(%%~a 1 1 1 %%~b %%~c
        )
    ))>infofile.txt
    type infofile.txt
    

    output example:

    C:\Users\Private\Pictures\snap001.png 1 1 1 528 384
    C:\Users\Private\Pictures\snap002.png 1 1 1 1920 1080
    C:\Users\Private\Pictures\snap003.png 1 1 1 617 316
    C:\Users\Private\Pictures\snap004.png 1 1 1 1920 1080
    C:\Users\Private\Pictures\snap005.png 1 1 1 514 346
    C:\Users\Private\Pictures\snap006.png 1 1 1 1920 1080
    C:\Users\Private\Pictures\snap007.png 1 1 1 395 429
    C:\Users\Private\Pictures\snap008.png 1 1 1 768 566
    C:\Users\Private\Pictures\snap009.png 1 1 1 1536 1080
    C:\Users\Private\Pictures\snap010.png 1 1 1 1600 480