1

I'm using imagemagick in my batch script to evaluate a folder of jpg and png files. I want to check the print size of each image, and if it meets the criteria I specified, then output to a text file. My code is not working how i would like. It will output all of the data to the text file, but only if every image in the folder meets the criteria for correct size. I want it to evaluate each individual image. Currently, if there is 1 image not within the criteria, then nothing is written to the text file.

@echo off

REM %f - filename
REM %[size] - original size of image
REM %W - page width
REM %[resolution.x] - X density (resolution) without units
REM %H - page height
REM %[resolution.y] - Y density (resolution) without units
REM \n - newline
REM 1 in = 0.393701 cm - for PNG cm to in conversion

set output="C:\Documents and Settings\mfishm000\Desktop\Image_size.txt"

echo File_name:Size:Width:Height > %output%

REM set width as variable
FOR /F %%x IN ('identify -format "%%[fx:W/resolution.x]" *.jpg') DO SET width=%%x
FOR /F %%x IN ('identify -format "%%[fx:W/resolution.x*0.393701]" *.png') DO SET width=%%x

REM set height as variable
FOR /F %%y IN ('identify -format "%%[fx:H/resolution.y]" *.jpg') DO SET height=%%y 
FOR /F %%y IN ('identify -format "%%[fx:H/resolution.y*0.393701]" *.png') DO SET height=%%y

REM check if width is less than 8.67 and height is less than 11.22. If yes then output them to txt file
IF %width% LSS 8.67 (
    IF %height% LSS 11.22 (
        identify -format "%%f:%%[size]:%%[fx:W/resolution.x]:%%[fx:H/resolution.y]\n" *.jpg >> %output%
        identify -format "%%f:%%[size]:%%[fx:W/resolution.x*0.393701]:%%[fx:H/resolution.y*0.393701]\n" *.png >> %output%
    )
)

1 Answer 1

2

Think the problem is you need to process each file individually. Seems like you end up with only one width/height value of the last file processed. Haven't tested, but this should work.

    @ECHO OFF

    REM %f - filename
    REM %[size] - original size of image
    REM %W - page width
    REM %[resolution.x] - X density (resolution) without units
    REM %H - page height
    REM %[resolution.y] - Y density (resolution) without units
    REM \n - newline
    REM 1 in = 0.393701 cm - for PNG cm to in conversion

    set output="C:\Documents and Settings\mfishm000\Desktop\Image_size.txt"

    echo File_name:Size:Width:Height > %output%


    for /f %%a in ('dir /b ^| find ".jpg"') do call :processA "%%~a"
    for /f %%a in ('dir /b ^| find ".png"') do call :processB "%%~a"

    exit /B

    :processA
        FOR /F %%x IN ('identify -format "%%[fx:W/resolution.x]" %1') DO SET width=%%x
        FOR /F %%y IN ('identify -format "%%[fx:H/resolution.y]" %1') DO SET height=%%y
        IF %width% LSS 8.67 (
            IF %height% LSS 11.22 (
                identify -format "%%f:%%[size]:%%[fx:W/resolution.x]:%%[fx:H/resolution.y]\n" %1 >> %output%
            )
        )
    goto:eof

    :processB
        FOR /F %%x IN ('identify -format "%%[fx:W/resolution.x*0.393701]" %1') DO SET width=%%x
        FOR /F %%y IN ('identify -format "%%[fx:H/resolution.y*0.393701]" %1') DO SET height=%%y
        IF %width% LSS 8.67 (
            IF %height% LSS 11.22 (
                identify -format "%%f:%%[size]:%%[fx:W/resolution.x*0.393701]:%%[fx:H/resolution.y*0.393701]\n" %1 >> %output%
            )
        )
    goto:eof
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.