How to identify a 24bit encoded jpeg but with no real 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
dragonv12

How to identify a 24bit encoded jpeg but with no real color?

Post by dragonv12 »

I try to identify all images which are scanned from grayscale pages but saved as 24bit images. I tried to count colors but I get :

Code: Select all

- colored page :   Colors: 163513
- grayscale page : Colors: 1646
So I don't see any rule I could apply there...
Anyone got an idea ? Eventually I could apply a filter to the images, CPU Time is not an issue :)

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

Re: How to identify a 24bit encoded jpeg but with no real color?

Post by fmw42 »

use:

convert yourimage -resize 1x1! -fx "u.r==u.g&&u.r==u.b?1:0" txt:-

If the image has all three channels the same, you will get a return of 1 or white. If the image has any channel different from the others, you will get 0 or black returned.


You can also try

convert yourimage -format "%[colorspace]" info:-

If the image is grayscale (as IM should recognize that all 3 channels are the same) then it will have colorspace returned as Gray; otherwise RGB
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to identify a 24bit encoded jpeg but with no real color?

Post by anthony »

JPEG images will never be 'gray' as it is lossy.

However a basic 'grey' test is, to compare the image against
a gray version of itself!
See http://imagemagick.org/Usage/compare/#type_general

Code: Select all

convert image.jpg  \( +clone -colorspace gray \)  \
          -compose difference -composite \
          -format '%[mean]' info:
You can also use '%[maximum]' instead.

Alternative...

Code: Select all

convert image.jpg \( +clone -colorspace gray \)  miff:- |
    compare -metric PAE - null:
the value given in parenthesis is a normalization of the maximum possible error (1.0 = max)

With the above rose: produced: 41120 (0.627451)
or about 62% error and thus NOT GRAY
while a granite: image produced: 2056 (0.0313725)
or about 3% error and thus may be considered to be GRAY
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
dragonv12

Re: How to identify a 24bit encoded jpeg but with no real color?

Post by dragonv12 »

I appreciate your help !!
I will try all your solutions and tell which one is most efficient :)
Post Reply