Get color of pixel with Magick++

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
Posts: 24
Joined: 2011-03-21T09:27:47-07:00
Authentication code: 8675308

Get color of pixel with Magick++

Post by ghostmansd » 2011-10-04T10:06:35-07:00

Hello to all forum users! I need your help. I'm writing a small application, which needs a possibility to get color of any pixel in image. How can I get it with Magic++?
Thank you very much for your answer!

Posts: 4
Joined: 2012-02-14T13:51:23-07:00
Authentication code: 8675308

Re: Get color of pixel with Magick++

Post by dc87 » 2012-02-15T08:21:42-07:00

Yes, refer to the Magick++ documentation on,
and for pixels in particular,
Look at bottom section, 'Low-level image pixel access'
The Pixels Class also provides pixel access, look at

Here some sample code, that converts all black pixels to blue and all others to yellow. Read from input.tif and Write to output.tif

Code: Select all

#include <Magick++.h>
#include <iostream>

using namespace std;
using namespace Magick;
int main(void) {

    Color black("black");
    Color blue("blue");
    Color yellow("yellow");
    Image tifImage("input.tif");
    size_t rows=1586, columns=1256;
    tifImage.classType( DirectClass );

    PixelPacket *tifPixels = tifImage.getPixels(0,0,columns,rows);
    for(ssize_t i=0; i<columns*rows; i++){
        if ( *(tifPixels+i) == black ){
           *(tifPixels+i) = blue;     
        } else {
            *(tifPixels+i) = yellow;
    return 0;
You can also get access to individual channels (RGBA) via other methods/attributes.

Code: Select all

        Quantum red, green, blue;        
        red = tifPixels[i].red;         // or tifPixels[i].redQuantum
        green = tifPixels[i].green;  // or tifPixels[i].greenQuantum
        blue = tifPixels[i].blue;      // or tifPixels[i].blueQuantum

Posts: 2
Joined: 2014-02-12T13:52:37-07:00
Authentication code: 6789

Re: Get color of pixel with Magick++

Post by Davide » 2014-02-12T14:13:36-07:00

Resuming this old but pertinent thread, I found myself in trouble when trying to extract the integer value of channel's pixels.

Here's my code. Cout always prints 65535, whatever image file is processed, whatever coordinates the pixel is picked from. Since the image is grayscale, I'd expect I should only need to extract color values from one channel.

Code: Select all

int main(int argc, char *argv[]) {
  Image img;
  ColorGray val;[1]);
  int w = img.columns();
  int h = img.rows();

  PixelPacket *pixels = img.getPixels(0, 0, w, h);

  val = pixels[w * 135 + 234];
  cout << "val: " << val.redQuantum() << endl;
Edit: "Solved". I happened to have a full set of completely white images over my text-only SSH.

Post Reply