Count white color

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
myspacee
Posts: 153
Joined: 2007-06-14T02:09:35-07:00

Count white color

Post by myspacee »

Hello,
Windows user, last version installed.

Reading this link searching a method to identify when my Production fail to generate a PDF file.
Think that counting how many WHITE there is i can easily identify bad file from good one.

Need to count how many white there is, maybe in percert value. Post thumb to help understand problem:
Image
http://static.repubblica.it/laprovincia ... rr/bad.jpg

and pdf files:
http://static.repubblica.it/laprovincia ... r/good.pdf
http://static.repubblica.it/laprovincia ... rr/bad.pdf

Find interesting reply in old post:
cconvert bad.pdf -fill white -opaque white -fill black +opaque white -format "%%[fx:100*mean]" info:

but can't understand how read value.

Anyone can help please?

thank you,
m.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Count white color

Post by fmw42 »

convert bad.pdf -fuzz XX% -fill white -opaque white -fill black +opaque white -format "%%[fx:100*mean]" info:
This changes any color within XX% of white to white (-fuzz XX% -fill white -opaque white), then changes anything that is not white to black (-fill black +opaque white), then computes the mean value (in range 0 to 1) of the binary image and multiplies by 100 to get percent white.
myspacee
Posts: 153
Joined: 2007-06-14T02:09:35-07:00

Re: Count white color

Post by myspacee »

thank you for reply fmw42,
create batch that analize files when created.

have a question, is there any way to cut decimal in % value [98.5866 --> 98] ?

thank you for support,
m.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Count white color

Post by Bonzo »

Try:

Code: Select all

convert bad.pdf -fuzz XX% -fill white -opaque white -fill black +opaque white -format "%%[fx:round(100*mean)]" info:
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Count white color

Post by anthony »

Bonzo wrote:Try:

Code: Select all

convert bad.pdf -fuzz XX% -fill white -opaque white -fill black +opaque white -format "%%[fx:round(100*mean)]" info:
On DOS you need the double %, under linux (or IMv7 "magick script") you do not.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Count white color

Post by whugemann »

I think that this question is rather more about DOS batch programming than about ImageMagick. So here is some batch sample code. For explanations see the link below. The code stores the mean value in an environment variable named WP, which may be evaluated by an IF statement. I kept the Convert statement rudimentary -- see the hints of the others in regard to it:

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
For %%i in (*.pdf) DO (
For /F %%a in ('convert %%i -resize 1x1 -format "%%[fx:100*mean]" info:') DO SET WP=%%a
IF !WP! GEQ 95 (
ECHO %%i = White
) ELSE (
ECHO %%i = Black
)
)
Wolfgang Hugemann
Post Reply