Page 1 of 2

Counting Color and Grey Scale in a directory?

Posted: 2019-09-10T09:07:35-07:00
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?

Re: Counting Color and Grey Scale in a directory?

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

Re: Counting Color and Grey Scale in a directory?

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

Re: Counting Color and Grey Scale in a directory?

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

Re: Counting Color and Grey Scale in a directory?

Posted: 2019-09-10T11:07:21-07:00
by bratpit
for i in *.png;do exiftool "$i"|grep Grayscale ;done|wc -l

Re: Counting Color and Grey Scale in a directory?

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

Re: Counting Color and Grey Scale in a directory?

Posted: 2019-09-16T10:49:47-07:00
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.

Re: Counting Color and Grey Scale in a directory?

Posted: 2019-09-17T00:04:27-07:00
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

Re: Counting Color and Grey Scale in a directory?

Posted: 2019-09-17T09:04:37-07:00
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

Re: Counting Color and Grey Scale in a directory?

Posted: 2019-09-17T11:13:33-07:00
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.

Re: Counting Color and Grey Scale in a directory?

Posted: 2019-09-17T11:27:34-07:00
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.

Re: Counting Color and Grey Scale in a directory?

Posted: 2019-09-17T12:05:13-07:00
by fmw42
Correct -- Colorspace is Gray and Type is Grayscale.

Re: Counting Color and Grey Scale in a directory?

Posted: 2019-09-19T00:29:43-07:00
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.

Re: Counting Color and Grey Scale in a directory?

Posted: 2019-09-19T12:30:49-07:00
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.

Re: Counting Color and Grey Scale in a directory?

Posted: 2019-09-19T14:25:17-07:00
by fmw42
What was your exact command?