pythonimagergbwandmagickwand

getting the average rgb value of an image in wand


I'm wondering if it is possible to calculate the average rgb value in with wand?

I know how to do it with PIL, but in the docs of wand I can't really find how to get the image data.

The only thing I could find was this:

for row in image:
    for col in row:
        assert isinstance(col, wand.color.Color)
        print(col)

But then col is a Color object and I'm not really sure how to extract the values from there.

Any ideas?


Solution

  • You seem to have answered the question with the information you provided :D

    if col is a Color object, then it's as easy as pulling information from the child node like this:

    col.red
    

    Here's my full code (using Python 2). I never used Wand, but this is definitely awesome!

    from wand.image import Image
    from wand.display import display
    from wand.color import Color
    
    with Image(filename='mona-lisa.png') as image:
        for row in image:
            for col in row:
                assert isinstance(col, Color)
                print str(col) + "R:"+str(col.red)+"|"+"G:"+str(col.green)+"|"+"B:"+str(col.blue)
    

    So, if you want the average, you can average the reds together, the greens or all of them.

    More on the nodes/modules of the Color object can be found here:

    Wand Documentation for Color object