Count color pages of TIFF image

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
lindsten

Count color pages of TIFF image

Post by lindsten »

Hi everyone,

I've just started to look at ImageMagick (or actually Magick++) so I apologize for any stupid questions...


I have a multipage TIFF image and need to count how many of the pages are colored and how many are grayscale (from a C++ program). My first thought was to get the number of total pages (don't know how yet) and then retrieve each page using the read("myfile.tiff[n]") syntax. For each page retrieved I call getConstPixels() and then iterate over all the pixels and compare the R, G and B values for each pixel with opacity != TransparentOpacity. If they're not equal the page contains color and I move on to the next page.

Is this the way to go or is there a better (talking performance) approach?


Btw, how do I get the total number of pages? I know how to do it from the command line (identify -format %n myfile.tiff), but what is the C++ equivalent?

/ Mikael
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Count color pages of TIFF image

Post by magick »

To get the number of images with Magick++ use a list which can return its length. To get you started:
  • list<Image> imageList;
    readImages( &imageList, "image.tiff" );
To determine of the image is grayscale you could use statistics() and compare the red, green, and blue channels. If they are equal, the image is gray.
lindsten

Re: Count color pages of TIFF image

Post by lindsten »

A list seems like a good start :D And I'll take a look at the statistics function you mention...

Thanks!


But first I have to get Magick++ working (viewtopic.php?f=1&t=12240)!

/ Mikael
lindsten

Re: Count color pages of TIFF image

Post by lindsten »

Can't seem to find any statistics function... where should I look?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Count color pages of TIFF image

Post by magick »

See http://www.imagemagick.org/Magick++/Image.html and look for the statistics() method description:
  • statistics
    ImageStatistics *statistics
    Obtain image statistics. Statistics are normalized to the range of 0.0 to 1.0 and are output to the specified ImageStatistics structure. The structure includes members maximum, minimum, mean, standard_deviation, and variance for each of these channels: red, green, blue, opacity (e.g. statistics->red.maximum).
lindsten

Re: Count color pages of TIFF image

Post by lindsten »

That's wierd!

I just had that very same page open and I did a search for statistics (several times actually) without hits. I opened your link in another tab and compared the two. In the first tab the statistics function was actually missing :shock: ...until I reloaded the page, then it appeared.

Is my browser playing a joke on me, or did you just update the page? :)


Anyway, thanks for the help!


/ Mikael
palpacino

Re: Count color pages of TIFF image

Post by palpacino »

Hi Guys,

For what it is worth, I just found a way to get TIF page count by using the Shell.Application object (in VB and WinXP in my case)...

Code: Select all

    Dim aryArray() As String
    Dim objShell
    Dim objFolder
    Dim strPC As String
    Dim strDM As String
    Dim objFileName
    Dim strRowSource As String
    
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.NameSpace("C:\FOLDER\")

    For Each objFileName In objFolder.Items
        strDM = objFolder.GetDetailsOf(objFileName, 3)
        strPC = objFolder.GetDetailsOf(objFileName, 13)
        strRowSource = Chr(34) & Format(CDate(strDM), "MM/DD/YY HH:mm AM/PM") & Chr(34) & ";" & Chr(34) & strPC & Chr(34) & ";" & strRowSource
    Next
There are actually 33 Details. They are the same ones you see in windows explorer when you right-click on the columns and select what columns you want to view. In my case, I just got the "Pages" one which is 13 and the date created which is 3.

Other info:
  • 0 Name
    1 Size
    2 Type
    3 Date Modified
    4 Date Created
    5 Date Accessed
    6 Attributes
    7 Status
    8 Owner
    9 Author
    10 Title
    11 Subject
    12 Category
    13 Pages
    14 Comments
    15 Copyright
    16 Artist
    17 Album Title
    18 Year
    19 Track Number
    20 Genre
    21 Duration
    22 Bit Rate
    23 Protected
    24 Camera Model
    25 Date Picture Taken
    26 Dimensions
    27 ?
    28 ?
    29 Episode Name
    30 Program Description
    31 ?
    32 Audio sample size
    33 Audio sample rate
jaffamuffin
Posts: 59
Joined: 2009-01-30T03:46:08-07:00

Re: Count color pages of TIFF image

Post by jaffamuffin »

removed/ mistakes...
Post Reply