How to count/list all images in a dir tree with certain size or resolution?
How to count/list all images in a dir tree with certain size or resolution?
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
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
-
- 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?
I would do it in a BAT script, like this:
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.
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%
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
Re: How to count/list all images in a dir tree with certain size or resolution?
@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?
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?
-
- 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?
IM cannot easily give a simple number that is the filesize in bytes. But the OS can, for example:
So you can incorporate the filesize in the above script like this:
Code: Select all
for %F in (*.jpg) do echo %~zF
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
- 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?
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).snibgo wrote:IM cannot easily give a simple number that is the filesize in bytes
For example:
Code: Select all
convert ip_X2.psd[0] -format "%b\n" info:
But adding -precision 16:
Code: Select all
convert -precision 16 ip_X2.psd[0] -format "%b\n" info:
-
- 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?
Oh, I didn't know that. That's good, thanks.fmw42 wrote:... you can use -precision to force it to show Bytes (B) ...
snibgo's IM pages: im.snibgo.com
-
- 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?
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
- 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?
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?
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
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
-
- 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?
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.bensto wrote:... I am experiencing errors like "Missing operator".
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
Re: How to count/list all images in a dir tree with certain size or resolution?
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"
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"
-
- 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?
There is no ??????? that can be used in "INC=%%[fx:???????>=100000]" for filesize in bytes.
snibgo's IM pages: im.snibgo.com
Re: How to count/list all images in a dir tree with certain size or resolution?
Ok, so "-precision 16" is completely useless since I get the number of Bytes already with "set BYTES=%%~zF"