How to check image color or back and 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?".
Hjkma
Posts: 7
Joined: 2015-03-07T15:48:52-07:00
Authentication code: 6789

Re: How to check image color or back and white

Post by Hjkma »

I did everything according to your instructions, the ruby is determined by my computer, installed a new version of ImageMagick, put the convert.exe in the image folder, put two ruby script there, ran a small one of them, and it only show error messages on the command line. Example:
convert: unable to open image ''100569513714.jpg'': No such file or directory @
error/blob.c/OpenBlob/3094.
convert: unable to open module file 'C:\Program Files\ImageMagick-7.0.5-Q16\modu
les\coders\IM_MOD_RL_JPG'_.dll': No such file or directory @ warning/module.c/Ge
tMagickModulePath/680.
convert: no decode delegate for this image format `JPG'' @ error/constitute.c/Re
adImage/509.
convert: no images defined `histogram:info:-' @ error/convert.c/ConvertImageComm
and/3254.
ERROR: histogram size (0) < 2!
convert: unable to open image ''100580631911.jpg'': No such file or directory @
error/blob.c/OpenBlob/3094.
convert: unable to open module file 'C:\Program Files\ImageMagick-7.0.5-Q16\modu
les\coders\IM_MOD_RL_JPG'_.dll': No such file or directory @ warning/module.c/Ge
tMagickModulePath/680.
convert: no decode delegate for this image format `JPG'' @ error/constitute.c/Re
adImage/509.
convert: no images defined `histogram:info:-' @ error/convert.c/ConvertImageComm
and/3254.
ERROR: histogram size (0) < 2!
darksndr
Posts: 7
Joined: 2016-06-30T13:11:30-07:00
Authentication code: 1151

Re: How to check image color or back and white

Post by darksndr »

This should work:
1. Open up a command prompt: Windows Menu > Run > type "cmd" and hit ENTER
2. type: cd "c:\path\to\your\images folder"
3. type ruby file_that_iterates_among_images.rb

if it doesn't then ask for help to some IT friend near you, to run this you must have some basic knowledge of the dos command prompt.
Hjkma
Posts: 7
Joined: 2015-03-07T15:48:52-07:00
Authentication code: 6789

Re: How to check image color or back and white

Post by Hjkma »

I did it, the same errors appear.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to check image color or back and white

Post by snibgo »

Hjkma wrote:I... put the convert.exe in the image folder ...
That won't work. After installation, you can't simply move the program around.
snibgo's IM pages: im.snibgo.com
Hjkma
Posts: 7
Joined: 2015-03-07T15:48:52-07:00
Authentication code: 6789

Re: How to check image color or back and white

Post by Hjkma »

But darksndr said that I can copy this file into a folder with images. I already did this, copied it to the image folder. I tried to move the images to the ImageMagick folder and run the script there, but also the same errors appear. It seems the problem is due to the incorrect installation of ImageMagick, but I do not know how to fix it. I tried to delete and reinstall the program, I checked all checkboxes at installing, but after reinstallation, these errors appear again.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to check image color or back and white

Post by snibgo »

In Windows, the portable version can be moved without problems, but not the other versions.

After installing, did you check that "convert" worked, from any directory?

Code: Select all

convert rose: r.png
convert rose: r.jpg
convert r.png r2.jpg
convert r.jpg r2.png
snibgo's IM pages: im.snibgo.com
Hjkma
Posts: 7
Joined: 2015-03-07T15:48:52-07:00
Authentication code: 6789

Re: How to check image color or back and white

Post by Hjkma »

snibgo, yes, it works, I created a batch file with your code in another folder, copied the convert.exe file there, ran the batch file and it created four files
r.jpg
r2.jpg
r.png
r2.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to check image color or back and white

Post by snibgo »

Okay, good, so ImageMagick works fine. I don't know ruby. Does the ruby script work now? I can't advise on ruby.
snibgo's IM pages: im.snibgo.com
BenjaminKim
Posts: 11
Joined: 2019-05-03T12:14:07-07:00
Authentication code: 1152

Re: How to check image color or back and white

Post by BenjaminKim »

Hello - I came across this topic in researching how to accomplish something I would like to try and accomplish.

I've been working through the various posts, and ideas, but can't seem to figure out the best way to do what I need to do. If it would be better to create a new topic, then I will.

Ultimately, I need to check a large collection of images to see if they have any pixels which are between 67% white and 99% white. Source images may be color (sRGB or aRGB), may be grayscale, or may be pure B&W.

I have a decent understanding of LAB colorspace, so worked out that if I "convert" the colorspace in memory, I can use the L channel to determine the white % of any color (in theory). However, I'm not certain how to best determine if any pixels between 67% white and 99% white exist in image. Even if there is 1 pixel in this range, then I need for that image to be flagged.

So, in essence - check each image to see if any pixels between 67% and 99% white exist - if so, report yes - if no, then either report no or nothing. I know how to build the loops and parse output as needed - I just can't figure out what exactly I need to parse.

I suspect that it is possible to leverage Imagemagick to accomplish this - I'll keep beating my head on it. In the meantime, any thoughts or advice are greatly appreciated.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to check image color or back and white

Post by fmw42 »

Generally better to start a new topic, in the future.

You can use -range-threshold if using Imagemagick 7. See viewtopic.php?f=4&t=34604

Otherwise, you can achieve that using

Code: Select all

convert input_image -black-threshold 67% -white-threshold 99% -fill black -opaque white -threshold 0 result_image
That will give you a white image if any values between 67% and 99% and black elsewhere.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to check image color or back and white

Post by snibgo »

If your image is grayscale, I expect Fred's answer is good.
BenjaminKim wrote:... if they have any pixels which are between 67% white and 99% white.
When the image is colour, then how do you define whiteness? What calculation do you want, to give a percentage whiteness? Perhaps you mean some scale of "lightness", eg L of Lab. If so, then convert to Lab, separate the L channel, then use Fred's solution.
snibgo's IM pages: im.snibgo.com
darksndr
Posts: 7
Joined: 2016-06-30T13:11:30-07:00
Authentication code: 1151

Re: How to check image color or back and white

Post by darksndr »

I think you can use the same trick as I did: get the histogram for the LAB colorspace and then check the L values of each row:

Code: Select all

convert fname.ext -colorspace LAB -format %c histogram:info:-
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to check image color or back and white

Post by fmw42 »

You can look for white in the product of B * (negate of S) in HSB. White will be bright and low saturation (or high values in the negate of S) image. Thus you can threshold that product image in with my commands above.
BenjaminKim
Posts: 11
Joined: 2019-05-03T12:14:07-07:00
Authentication code: 1152

Re: How to check image color or back and white

Post by BenjaminKim »

Thank you a million times over! This definitely helps point me in the right direction.

It may also be exactly the solution - however, I'm still chewing on the color aspect of it (the source images may be color, or may be not). I'm confident re gray "colors" in a RGB colorspace - for instance, rgb(171, 171, 171) (which is ~67% white), however, I'm still working out how to be be confident of the "lightness" (or "whiteness") of something like rgb(51, 204, 255). In theory, I think converting to LAB, and then polling L channel will do it, but need to do more detailed tests - especially around the "edges" (for instance, rgb(84, 216, 255) vs rgb(85, 216, 255).

Further, HSB is new to me - but studied it and all the material I could find written so far re ImageMagick and colors. For instance, why HSB and not HSL? I'm sure the info is there, and figuring out why on my own will help ensure I firmly understand.

I'm working on ways to reverse processes recommended above using LAB and/or HSB (and/or HSL) to establish the "edge" ranges in RGB - the ones that borderline on 66% "lightness" and 67% "lightness", and thus give me a sample set to visualize and test against.

Again, many, many thanks for providing thoughts and direction. I'll post an update once I have it finalized.
BenjaminKim
Posts: 11
Joined: 2019-05-03T12:14:07-07:00
Authentication code: 1152

Re: How to check image color or back and white

Post by BenjaminKim »

One additional question:

How do I factor in the fact that most of the "source" files also have an alpha layer?

I've uploaded an example: samplepdf0001.pdf (plus the identity verbose output).

If I run: magick identify -verbose samplepdf0001.pdf > samplepdf0001.txt

And then look at the histogram info, I can see the examples of the colored text: 248,203,173

There are various alpha values assigned - I'm guessing these are the lighter "edges" of the colored text. For instance, if I use color sampler, I see (251,229,214) values in the pixels immediately left of the colored "i" characters (or really the far left edge of any of the colored characters).

However, I do not see any values for the gray "SAMPLE" stamped across the page. Colorsampler tells me it's mostly 202,202,202 - however, that's not showing up in the histogram data. And I can't seem to work out a way to calculate what the resulting "RGB" value is for a RGBA value. (Or a LAB for a LABA, or HSB for a HSBA, etc....)

PDF: https://www.dropbox.com/sh/2jfzocc4tmdk ... rIjKa?dl=0

PDF identify verbose output: https://www.dropbox.com/s/wh04dbpms4roh ... 1.txt?dl=0

I think that this is the part that has really been throwing me off - things weren't adding up until I realized the alpha aspect.

Also:
When I look at a site like: http://colorizer.org/ (or any site that generates converts various colors from one format to another) - they have options like ARGB, RGBA ABGR, BGRA with numbers assigned (towards bottom of all the colorspaces, under "More Conversions" tab. Sliding alpha or changing RGB values changes the ARGB or RGBA values.

For instance: RGB(248,203,173) Alpha 1:

ARGB = -472147
RGBA = -120869377

If I keep RGB(248,203,173) but change alpha to "70", then the ARGB and RGBA values change:

ARGB = -1275540563
RGBA = -120869453

Maybe there is a way to leverage these values - as each combination of colorspace value (RGB, LAB, HSB) plus alpha adjustment seems to generate a unique value. I'm not sure how it's generating these, but maybe someone knows.

I'll keep digging, but if anyone has thoughts, then greatly appreciated. Many thanks, again.
Post Reply