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

Counting Color and Grey Scale in a directory?

Post by ccameron » 2019-09-10T09:07:35-07:00

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: 12002
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 » 2019-09-10T09:10:47-07:00

"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: 25392
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 » 2019-09-10T09:15:19-07:00

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: 12002
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 » 2019-09-10T10:31:34-07:00

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: 13
Joined: 2018-09-16T00:20:21-07:00
Authentication code: 1152

Re: Counting Color and Grey Scale in a directory?

Post by bratpit » 2019-09-10T11:07:21-07:00

for i in *.png;do exiftool "$i"|grep Grayscale ;done|wc -l

ccameron
Posts: 2
Joined: 2019-09-09T13:33:20-07:00
Authentication code: 1152

Re: Counting Color and Grey Scale in a directory?

Post by ccameron » 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.

User avatar
fmw42
Posts: 25392
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 » 2019-09-16T10:49:47-07:00

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: 13
Joined: 2018-09-16T00:20:21-07:00
Authentication code: 1152

Re: Counting Color and Grey Scale in a directory?

Post by bratpit » 2019-09-17T00:04:27-07:00

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

Post Reply