Counting Color and Grey Scale in a directory?

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?".
ccameron
Posts: 5
Joined: 2019-09-09T13:33:20-07:00
Authentication code: 1152

Counting Color and Grey Scale in a directory?

Post by ccameron »

I'm not sure if this has been asked before but is it possible to have Image Magic count images in a directory that are color or grey scale without having to open all of the files?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Counting Color and Grey Scale in a directory?

Post by snibgo »

"Without having to open all of the files?" No. There is nothing external to a file that says whether it has colour, unless some software keeps some kind of database.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Counting Color and Grey Scale in a directory?

Post by fmw42 »

If all the files are PNG, which distinguishes between RGB and Gray, you can read the colorspace and/or type from the headers, I think. So the raster data would not need to be read, only the headers. This would be quicker. Other tools such as EXIFTOOL, might be faster. Snibgo would know more about all this than I.

For example:

Code: Select all

convert -ping image -format "%[colorspace]" info:
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Counting Color and Grey Scale in a directory?

Post by snibgo »

Yes, exiftool and ImageMagick can both do the job. They would both open each file and then read metadata, and possibly every pixel.
snibgo's IM pages: im.snibgo.com
bratpit
Posts: 17
Joined: 2018-09-16T00:20:21-07:00
Authentication code: 1152

Re: Counting Color and Grey Scale in a directory?

Post by bratpit »

for i in *.png;do exiftool "$i"|grep Grayscale ;done|wc -l
ccameron
Posts: 5
Joined: 2019-09-09T13:33:20-07:00
Authentication code: 1152

Re: Counting Color and Grey Scale in a directory?

Post by ccameron »

Would this still work with .Tif files? I am new to these tools and have not used them before.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Counting Color and Grey Scale in a directory?

Post by fmw42 »

I think you can do it with TIFF also. But not JPG. I do not think JPG keeps a record of Grayscale in its header.
bratpit
Posts: 17
Joined: 2018-09-16T00:20:21-07:00
Authentication code: 1152

Re: Counting Color and Grey Scale in a directory?

Post by bratpit »

ccameron wrote: 2019-09-16T10:31:02-07:00 Would this still work with .Tif files? I am new to these tools and have not used them before.

Code: Select all

for i in *.jpg;do identify -ping "$i"|grep Grayscale ;done|wc -l
for i in *.tif;do identify -ping "$i"|grep Grayscale ;done|wc -l
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Counting Color and Grey Scale in a directory?

Post by fmw42 »

bratpit wrote: 2019-09-17T00:04:27-07:00

Code: Select all

for i in *.jpg;do identify -ping "$i"|grep Grayscale ;done|wc -l
for i in *.tif;do identify -ping "$i"|grep Grayscale ;done|wc -l
Grep from identify for Grayscale is wrong. It would need to be Gray

convert logo: -colorspace gray logo_gray.jpg
identify logo_gray.jpg
logo_gray.jpg JPEG 640x480 640x480+0+0 8-bit Gray 256c 34253B 0.000u 0:00.001
bratpit
Posts: 17
Joined: 2018-09-16T00:20:21-07:00
Authentication code: 1152

Re: Counting Color and Grey Scale in a directory?

Post by bratpit »

Yes

But this is some inaccuracy in display fields by identify IMHO.

Output from tif:
convert logo: -colorspace gray logo_gray.tif
logo_gray.tif TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 33344B 0.000u 0:00.000

Both tif and jpg have field
type:Grayscale
from full verbose identify output.
bratpit
Posts: 17
Joined: 2018-09-16T00:20:21-07:00
Authentication code: 1152

Re: Counting Color and Grey Scale in a directory?

Post by bratpit »

bratpit wrote: 2019-09-17T11:13:33-07:00 Yes

But this is some inaccuracy in display fields by identify IMHO.

Output from tif:
convert logo: -colorspace gray logo_gray.tif
logo_gray.tif TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 33344B 0.000u 0:00.000

Both tif and jpg have field
type:Grayscale
from full verbose identify output.
So most safe version with grep for all formats will be Your from previous post:

Code: Select all

for i in *.jpg;do convert -ping "$i" -format "%[colorspace]" info:|grep Gray ;done|wc -l
for i in *.tif;do convert -ping "$i" -format "%[colorspace]" info:|grep Gray ;done|wc -l
for i in *.png;do convert -ping "$i" -format "%[colorspace]" info:|grep Gray ;done|wc -l

Sorry . I quoted my previous post.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Counting Color and Grey Scale in a directory?

Post by fmw42 »

Correct -- Colorspace is Gray and Type is Grayscale.
bratpit
Posts: 17
Joined: 2018-09-16T00:20:21-07:00
Authentication code: 1152

Re: Counting Color and Grey Scale in a directory?

Post by bratpit »

fmw42 wrote: 2019-09-17T12:05:13-07:00 Correct -- Colorspace is Gray and Type is Grayscale.
Yes but I am not telling about that.

I am telling about comparison two identify outputs for tiff and jpg.

logo_gray.jpg JPEG 640x480 640x480+0+0 8-bit Gray 256c 34253B 0.000u 0:00.001
logo_gray.tif TIFF 640x480 640x480+0+0 8-bit Grayscale Gray 33344B 0.000u 0:00.000

For me it is some kind of inconsistency.
ccameron
Posts: 5
Joined: 2019-09-09T13:33:20-07:00
Authentication code: 1152

Re: Counting Color and Grey Scale in a directory?

Post by ccameron »

Ok, I was able to get both tools working but how do I get it to drill into a multipage Tiff? It seems to be only reading the header information of the file or the initial image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Counting Color and Grey Scale in a directory?

Post by fmw42 »

What was your exact command?
Post Reply