How to get the image brightness?

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
swp
Posts: 3
Joined: 2013-09-06T11:06:54-07:00
Authentication code: 6789

How to get the image brightness?

Post by swp »

Hello,

I'm trying to determine the brightness of an image using the MagickWand programming interface. I managed to get a brightness value on the command line with the following command:
$ convert image.jpg -colorspace hsb -resize 1x1 txt:-
# ImageMagick pixel enumeration: 1,1,255,hsb
0,0: ( 55, 33,197) #3721C5 hsb(21.413%,12.9335%,77.1542%)
I am interested in the brightness value (i.e. 77.1542%), but I don't know how to get this value with the API. I have come this far:

Code: Select all

/* Use ImageMagick to get the image brightness */
int get_image_brightness(const char *path) {
    MagickBooleanType status;
    MagickWand *magick_wand;

    MagickWandGenesis();
    magick_wand = NewMagickWand();
    status = MagickReadImage(magick_wand, path);
    if (status == MagickFalse)
        return -1;
    MagickSetColorspace(magick_wand, HSBColorspace);
    MagickResizeImage(magick_wand, 1, 1, MitchellFilter, 1);

    /* TODO: Get brightness value here */

    magick_wand = DestroyMagickWand(magick_wand);
    MagickWandTerminus();

    return 0;
}
Which MagickWand function should I use to get that value?

I'm not sure if the above method to get the image brightness is good. If anyone knows a better method to get the brightness value, please tell me.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: How to get the image brightness?

Post by el_supremo »

I haven't tried this and also haven't used HSB colour space, but I think this will extract what you want:

Code: Select all

	PixelWand *pw;
	float percent;
	int r,g,b;
	
	MagickGetImagePixelColor(magick_wand,0,0,pw);
	r = PixelGetRedQuantum(pw);
	g = PixelGetGreenQuantum(pw);
	b = PixelGetBluequantum(pw);
	percent = (float)b/MagickQuantumRange;
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
swp
Posts: 3
Joined: 2013-09-06T11:06:54-07:00
Authentication code: 6789

Re: How to get the image brightness?

Post by swp »

Thank you very much Pete! Your code contained some typos, but after fixing those, I got the wrong value (it returned the blue color of the pixel wand, not the brightness). But your code example did point me to the right section of the documentation where I found the function PixelGetHSL(), which is just what I needed! Here is the result:

Code: Select all

/* Return image info for image `path` */
int get_image_brightness(const char *path) {
    MagickBooleanType status;
    MagickWand *magick_wand;
    PixelWand *pw;
    //size_t qrange = 0;
    //double percent;
    double hue, saturation, lightness;

    MagickWandGenesis();
    magick_wand = NewMagickWand();
    pw = NewPixelWand();
    status = MagickReadImage(magick_wand, path);
    if (status == MagickFalse)
        return -1;
    MagickSetColorspace(magick_wand, HSBColorspace);
    MagickResizeImage(magick_wand, 1, 1, MitchellFilter, 1);

    // Get the brightness value
    status = MagickGetImagePixelColor(magick_wand, 0, 0, pw);
    if (status == MagickFalse) {
        return -1;
    }
    PixelGetHSL(pw, &hue, &saturation, &lightness);
    //MagickGetQuantumRange(&qrange);
    //percent = lightness / (double)qrange;
    printf("brightness: %f\n", lightness);

    magick_wand = DestroyMagickWand(magick_wand);
    MagickWandTerminus();

    return 0;
}
It returns slightly different values than the `convert' command I used, but that's probably a result from using a different resize filter. I don't know which resize filter `convert' uses, but if anyone knows, please let me know. However I read somewhere in the forums that Mitchell is the default filter for shrinking.

Also I commented out the code for calculating the Quantum Range; I'm guessing it's not needed now.

If anyone knows a better way to calculate the brightness, please let me know.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to get the image brightness?

Post by fmw42 »

I believe that lanczos is used for shrinking and mitchell for enlarging. See http://www.imagemagick.org/Usage/filter/#default_filter
Post Reply