Page 1 of 1

How to count/list all images in a dir tree with certain size or resolution?

Posted: 2018-05-25T03:43:50-07:00
by bensto
Assume I have I big dirextory tree with lots of images inside.
The top node starts at the dir D:\archive\images\personal\

Now I want to count (resp. to log into logfile) all *.jpg images (only) which have a file size of e.g. 300KB and more.

Secondly I want to do a similar search but this time I want to count/list all files (*.img, *.png and others) which have a minimum
height of 200 pixel and/or a minimum width of 400 pixel.

How can I achieve this with ImageMagick command?

Thank you
Ben

Re: How to count/list all images in a dir tree with certain size or resolution?

Posted: 2018-05-25T04:13:19-07:00
by snibgo
I would do it in a BAT script, like this:

Code: Select all

set TOTAL=0
set COUNT=0

for /R . %%F in (*.jpg) do (

  for /F "usebackq" %%L in (`%IMG7%magick identify ^
    -ping ^
    -format "INC=%%[fx:w>=400||h>=200?1:0]" ^
    %%F`) do set %%L

  set /A TOTAL += 1
  set /A COUNT += !INC!
)

echo TOTAL=%TOTAL%
echo COUNT=%COUNT%
This tells me how many jpg files there are (TOTAL) in the directory tree starting from the current directory (".") and how many of those fulfil the condition (COUNT). The condition is that either the width is at least 400 pixels or that the height is at least 200. For each file found, INC is set to 1 if the condition is true, otherwise INC is set to 0. Change the condition to whatever you want. See http://www.imagemagick.org/script/escape.php

Sadly, IM won't give a simple number that is the filesize in bytes. It always gives a "human-friendly" version with suffix KB or MB or whatever. The operating-system "%~zF" can do that.

Re: How to count/list all images in a dir tree with certain size or resolution?

Posted: 2018-05-28T06:13:18-07:00
by bensto
@snibgo:

Ok, thank you.
You said:

>The operating-system "%~zF" can do that

What does that mean? Can I switch somehow from inside the batch script the format how the WinOS returns the number of bytes?

Re: How to count/list all images in a dir tree with certain size or resolution?

Posted: 2018-05-28T06:29:53-07:00
by snibgo
IM cannot easily give a simple number that is the filesize in bytes. But the OS can, for example:

Code: Select all

for %F in (*.jpg) do echo %~zF
So you can incorporate the filesize in the above script like this:

Code: Select all

set TOTAL=0
set COUNT=0

for /R . %%F in (*.jpg) do (

  set BYTES=%%~zF

  for /F "usebackq" %%L in (`%IMG7%magick identify ^
    -ping ^
    -format "INC=%%[fx:w>=400||h>=200?1:0||!BYTES!>=1000]" ^
    %%F`) do set %%L

  set /A TOTAL += 1
  set /A COUNT += !INC!
)

echo TOTAL=%TOTAL%
echo COUNT=%COUNT%

Re: How to count/list all images in a dir tree with certain size or resolution?

Posted: 2018-05-28T10:35:13-07:00
by fmw42
snibgo wrote:IM cannot easily give a simple number that is the filesize in bytes
Actually, you can use -precision to force it to show Bytes (B) for current versions of ImageMagick (IM 6.9.8.4 or higher and IM 7.0.5.5 or higher).

For example:

Code: Select all

convert ip_X2.psd[0] -format "%b\n" info:
24.5226MB

But adding -precision 16:

Code: Select all

convert -precision 16 ip_X2.psd[0] -format "%b\n" info:
24522605B

Re: How to count/list all images in a dir tree with certain size or resolution?

Posted: 2018-05-28T14:22:13-07:00
by snibgo
fmw42 wrote:... you can use -precision to force it to show Bytes (B) ...
Oh, I didn't know that. That's good, thanks.

Re: How to count/list all images in a dir tree with certain size or resolution?

Posted: 2018-05-28T15:19:32-07:00
by snibgo
EDIT: Not so good. With a sufficiently large precision, the text output is a count of bytes, but it is a string (not a simple number) with suffix "B" so it can't be used in an %[fx:] expression.

Re: How to count/list all images in a dir tree with certain size or resolution?

Posted: 2018-05-28T16:02:48-07:00
by fmw42
In Unix, you can easily strip the B off the end of the value piping to sed or some other variable tools.

Re: How to count/list all images in a dir tree with certain size or resolution?

Posted: 2018-05-30T00:21:24-07:00
by bensto
Thank you for all the suggestions.

However when I try to setup a full script I am experiencing errors like "Missing operator".

Furthermore I wonder how I can use the "-precision 16" parameter to filter and count the files matching the threshold.

Have a look at the following batch script:

http://s000.tinyupload.com/index.php?fi ... 3289934196

Whats wrong?

Thank you
Ben

Re: How to count/list all images in a dir tree with certain size or resolution?

Posted: 2018-05-30T06:09:01-07:00
by snibgo
bensto wrote:... I am experiencing errors like "Missing operator".
Then you have one or more bugs. That message comes from "set /A" commands where the expression uses a variable that hasn't been set.

You are using ImageMagick to find the size of all your files (*.*), but are they all images? If some are not, then this will fail and !INC! will not be set.

You use IM's "size" for the filesize. But I point out above that this gives a suffix like B or KB so you can't compare it to a number. As Fred says, you can insert "-precision 16" before the "-format" so the suffix is always "B", but that doesn't solve the problem.

You could save the size into a variable and strip the B and use that, or do as I showed you above with %%~zF etc.

Re: How to count/list all images in a dir tree with certain size or resolution?

Posted: 2018-06-11T20:24:58-07:00
by bensto
Ok, meanwhile I have fixed the bug.
The script runs for filtering images for width and height.

Now there is still a problem with the size.

Originally in one of the first posts the "set BYTES=%%~zF" trick was suggested.
Then fmw42 suggested -precision 16 solution.

From my point of view that means I can omit the "set BYTES=%%~zF" and replace it by IM internal size reference
So how does the size filtering work then?

..... identify -ping -precision 16 -format "INC=%%[fx:???????>=100000]" ......

I need the corresponding keyword for the filesize.
"w" stands for width, "h" stands for height and what .... for filesize?
"size" as above does not work"

Re: How to count/list all images in a dir tree with certain size or resolution?

Posted: 2018-06-12T01:40:53-07:00
by snibgo
There is no ??????? that can be used in "INC=%%[fx:???????>=100000]" for filesize in bytes.

Re: How to count/list all images in a dir tree with certain size or resolution?

Posted: 2018-06-12T02:09:21-07:00
by bensto
Ok, so "-precision 16" is completely useless since I get the number of Bytes already with "set BYTES=%%~zF"