Find images that are almost entirely blank white

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
JAY6390
Posts: 7
Joined: 2017-07-11T13:56:01-07:00
Authentication code: 1151

Find images that are almost entirely blank white

Post by JAY6390 »

Hi

I've used ImageMagick to find pure white images using the command line (linux) without an issue, however I notice that some images are also almost white. The command I used was
identify -verbose -unique
And count the colours. If it was 1 it was assumed pure white (as we don't have any other colours). Is there a way of finding these images with slight artifacts on them using IM?

Kind regards
Jay
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Find images that are almost entirely blank white

Post by fmw42 »

Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at viewtopic.php?f=1&t=9620

The easiest way I can think is to do:

convert image -fuzz XX% -fill black +opaque white -fill white -opaque white -format "%[fx:100*mean]" info:

The result will be a value between 0% and 100%. The closer to 100% the more white you have. The -fuzz XX% will allow colors close to white to be treated as white. A value of 0% would mean keep only perfectly white values.
JAY6390
Posts: 7
Joined: 2017-07-11T13:56:01-07:00
Authentication code: 1151

Re: Find images that are almost entirely blank white

Post by JAY6390 »

Hi

Thanks for the reply. Sorry for not giving the full info.
Version: ImageMagick 6.7.2-7 2017-03-22 Q16 http://www.imagemagick.org on centos 6
I'll give the fuzz option a shot and see what it comes up with thanks

Kind regards
Jay
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Find images that are almost entirely blank white

Post by GeeMack »

JAY6390 wrote: 2017-07-11T14:19:56-07:00I've used ImageMagick to find pure white images using the command line (linux) without an issue, however I notice that some images are also almost white. The command I used was
identify -verbose -unique
And count the colours. If it was 1 it was assumed pure white (as we don't have any other colours). Is there a way of finding these images with slight artifacts on them using IM?
If you just need to determine how near an image is to pure white, you can use IM to find its total average brightness by resizing it to 1x1, then testing that pixel for luma. A command like this should work for IM v6...

Code: Select all

convert input.png[1x1!] -format "%[fx:100*luma]" info:
That will output a value between 0 and 100, zero being all black and 100 being all white. You'll have to decide where your threshold is for the "slight artifacts".
JAY6390
Posts: 7
Joined: 2017-07-11T13:56:01-07:00
Authentication code: 1151

Re: Find images that are almost entirely blank white

Post by JAY6390 »

Nice info thanks GeeMack
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Find images that are almost entirely blank white

Post by GeeMack »

JAY6390 wrote: 2017-07-11T15:46:05-07:00Nice info thanks GeeMack
The FX expressions are extremely helpful for doing various types of analyses. You can find some good basic instructions at THIS link. You can even use conditional expressions in them, so you could, for example, test a whole directory of images with a command like this...

Code: Select all

convert *.jpg[1x1!] -format "%[fx:luma>0.98?1:0] %[f]\n" info: > tmp.txt
That would read every JPG in the directory as a resized 1x1 image, test them for a luminance value of 98%, and output each filename preceded with a 0 if the luma is less than 98%, or a 1 if the luma is 98% or more.

In a *nix shell or script you could pipe the output of the above command through "sed" to get only lines that start with 1, and even peel off the 1 so you get a text file list of all the JPGs in the directory with a luma value more than 98%.
JAY6390
Posts: 7
Joined: 2017-07-11T13:56:01-07:00
Authentication code: 1151

Re: Find images that are almost entirely blank white

Post by JAY6390 »

Awesome thank you GeeMack very much. I'll see if I can get a bash script going that'll do that for the some 1/4 million images it needs to check!
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Find images that are almost entirely blank white

Post by GeeMack »

JAY6390 wrote: 2017-07-11T16:49:55-07:00I'll see if I can get a bash script going that'll do that for the some 1/4 million images it needs to check!
I just found that at least in v6.7.7 and older versions of IM6, the FX expression "luma" isn't available, but "luminance" is. It seems to do exactly the same thing. I ran a command like this in a bash shell under Windows 10. I ran it in a directory of almost 2000 JPG images...

Code: Select all

convert *.jpg[1x1\!] -format '%[fx:luminance>0.98?1:0] %[f]\n' info: | sed -n 's/^1 \(.*\)/\1/p' > tmp.txt
In under a minute it generated a text file with all filenames of images with more than a 98% "luminance" value.
JAY6390
Posts: 7
Joined: 2017-07-11T13:56:01-07:00
Authentication code: 1151

Re: Find images that are almost entirely blank white

Post by JAY6390 »

Awesome. As it's a client server, I daren't update the version of IM, so will take a crack at that tomorrow and post back with the results :-)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Find images that are almost entirely blank white

Post by fmw42 »

Luma and luminance are different. The first is sRGB (non-linear) and the second is linear. In very old versions of Imagemagick, they were even swapped.

The reason I did not go to luma or grayscale or even simply fx:mean or even -threshold is that it does not consider color. That is why I simply tried to isolate near-white vs everything else. There may not be much difference when you get really close to white (very bright), but I thought this would be the best approach.

EDITED to remove an incorrect statement.
JAY6390
Posts: 7
Joined: 2017-07-11T13:56:01-07:00
Authentication code: 1151

Re: Find images that are almost entirely blank white

Post by JAY6390 »

Hmm - might have to go the separate server route then and compile a more recent version. Do either of you know which version I'll be safe to use that'll have the luma?
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Find images that are almost entirely blank white

Post by GeeMack »

JAY6390 wrote: 2017-07-11T18:06:01-07:00Hmm - might have to go the separate server route then and compile a more recent version. Do either of you know which version I'll be safe to use that'll have the luma?
You shouldn't have to do any upgrading if the command fmw42 provided works. Try his example and just modify that "-format" part...

Code: Select all

... -format '%[fx:100*mean]' ...
... to something like this...

Code: Select all

... -format '%[fx:mean>0.98?1:0] %[f]\n' ...
Then pipe it through the "sed" command from my example. You should get pretty much the same result. With any of these ideas you'll still need to try a few sample images to determine your own needs for the not-quite-white threshold.
JAY6390
Posts: 7
Joined: 2017-07-11T13:56:01-07:00
Authentication code: 1151

Re: Find images that are almost entirely blank white

Post by JAY6390 »

I just went with the upgrade to be sure - wasn't too much extra work. Found some images which was the main thing, and removed them. Thanks for all the help - much appreciated fmw42 and GeeMack
Post Reply