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

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
bensto
Posts: 31
Joined: 2012-07-02T00:32:10-07:00
Authentication code: 13

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
bensto
Posts: 31
Joined: 2012-07-02T00:32:10-07:00
Authentication code: 13

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

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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%
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

In Unix, you can easily strip the B off the end of the value piping to sed or some other variable tools.
bensto
Posts: 31
Joined: 2012-07-02T00:32:10-07:00
Authentication code: 13

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
bensto
Posts: 31
Joined: 2012-07-02T00:32:10-07:00
Authentication code: 13

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

Post 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"
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo »

There is no ??????? that can be used in "INC=%%[fx:???????>=100000]" for filesize in bytes.
snibgo's IM pages: im.snibgo.com
bensto
Posts: 31
Joined: 2012-07-02T00:32:10-07:00
Authentication code: 13

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

Post by bensto »

Ok, so "-precision 16" is completely useless since I get the number of Bytes already with "set BYTES=%%~zF"
Post Reply