GIMP Chat
http://gimpchat.com/

Get average color of selection
http://gimpchat.com/viewtopic.php?f=9&t=20645
Page 1 of 1

Author:  trandoductin [ Wed Nov 22, 2023 11:49 am ]
Post subject:  Get average color of selection

This need came up before I had script for it but it was lost in the other dead forum.
So I am doing this again and sharing it here:
Required by this post: Is there a way I can average and blend colors of... (on reddit)
This plug-in simply gets average RGB values using histogram of active layer (and of selection area if once exists) and sets the average RGB as Context ForeGround Color so you can work with that color however you want.
Menu Location: Python-Fu/Get Average Color.
You can set a short cut key to it if you use it often.
Here's the code (attached zipped .py as well):
#!/usr/bin/env python

# get-average-color.py
# Created by TT
# Trying to help solve this problem: https://old.reddit.com/r/GIMP/comments/17nxc75/is_there_a_way_i_can_average_and_blend_colors_of/
# Comments to gimpchat.com or gimp-forum.net
# License: GPLv3
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# To view a copy of the GNU General Public License
# visit: http://www.gnu.org/licenses/gpl.html
#
#
# ------------
#| Change Log |
# ------------
# Rel 1: Initial release
from gimpfu import *

def get_average_color(img, layer):
    # #Set up an undo group, so the operation will be undone in one step.
    #pdb.gimp_undo_push_group_start(img)
    mean_r,std_dev,median,pixels,count,percentile = pdb.gimp_drawable_histogram(layer,HISTOGRAM_RED,0.0,1.0)
    mean_g,std_dev,median,pixels,count,percentile = pdb.gimp_drawable_histogram(layer,HISTOGRAM_GREEN,0.0,1.0)
    mean_b,std_dev,median,pixels,count,percentile = pdb.gimp_drawable_histogram(layer,HISTOGRAM_BLUE,0.0,1.0)
    pdb.gimp_context_set_foreground((int(mean_r),int(mean_g),int(mean_b)))
    # pdb.gimp_undo_push_group_end(img)
    # #Ensure the updated image is displayed now
    # pdb.gimp_displays_flush()

register(
    "python_fu_get_average_color",
    "Get Average Color and sets it foreground color",
    "Get Average Color and sets it foreground color",
    "TT",
    "TT",
    "November 22, 2023",
    "Get Average Color",
    "RGB*",      # Alternately use RGB, RGB*, GRAY*, INDEXED etc.
    [
    #INPUT BEGINS
    (PF_IMAGE, "img", "Image", None),
    (PF_DRAWABLE,   "layer", "Drawable", None),
    #INPUT ENDS
    ],
    [],
    get_average_color,
    menu="<Image>/Python-Fu")
main()


Attachments:
File comment: compressed .py file
get-average-color.zip [1.14 KiB]
Downloaded 75 times

Author:  trandoductin [ Wed Nov 22, 2023 12:54 pm ]
Post subject:  Re: Get average color of selection

Just thought I'd share this too...
Image
I used this as the 2nd image trying to get people to buy my gig on fiverr hehe.

Author:  MareroQ [ Wed Nov 22, 2023 2:11 pm ]
Post subject:  Re: Get average color of selection

Have you forgotten?

The OLD Archive :gaah
https://www.gimpscripts.net/p/gimpscript.html

Attachments:
GLarchive.jpg
GLarchive.jpg [ 58.54 KiB | Viewed 2701 times ]

Author:  trandoductin [ Wed Nov 22, 2023 3:02 pm ]
Post subject:  Re: Get average color of selection

I didn't even know that existed. hehe

Author:  ofnuts [ Wed Nov 22, 2023 5:06 pm ]
Post subject:  Re: Get average color of selection

Sorry but this doesn't really work :geek: :

* Create a 2x2 pixel image with a diagonal of red and one of green.
* Use it as a pattern to fill half of an image.
* Get the average color of that part, you get (.5,.5,0) or (127.5,127.5,0) on a 0..255 scale
* Fill the other half of the image with (127,127,0) or (128,128,0) and you will see that this is much darker than the red/green pattern.

This is our old gamma-correction friend at play... If you still have Gimp 2.8 around make a red/green gradient and you will see that the middle of the gradient, instead of being yellow, is quite brown (and is indeed (127,127,0)). This is something that has been fixed by moving to "linear light" pressing in 2.10.

For correct values:

* Gamma is 2.24
* 1/Gamma is 0.4464,
* 0.5^0.4464=0.734 (187 on a 0..255 scale)
* Now fill the other half with (187,187,0) and you will see it is much closer to the red/green pattern (how much closer depends a bit on your display setting/viewing software, it is near perfect on the laptop screen (Gimp, Gwenview and Firefox) a bit less so on my big (uncalibrated) display.

See attachment for your own testing (but check only the 100% image, at smaller scales, the scaling interpolation will do the same mistakes(*)...). You can so the same with a black and white checkerboard pattern and you will find that the average gray is indeed (187,187,187).

Of course, if you have "continuous" colors, especially in a limited range, the average from the script is a bit closer to the correct value, and evaluating what should be the equivalent color is not as obvious anyway.

(*) On Gimp 2.8, scaling the text image to 400x400 makes the grid half the same color as the (127,127,0) part. Doing the same on 2.10 correctly keeps the grid half the same color as (187,187,0).

Attachments:
ColorAverage.png
ColorAverage.png [ 39 KiB | Viewed 2683 times ]

Author:  trandoductin [ Wed Nov 22, 2023 6:11 pm ]
Post subject:  Re: Get average color of selection

What I am confused doesn't gimp_histogram do mean meaning mean? What's the error in logics since I can't see any.
Or if you mean blend based on perception then there's no way for me to do that accurately.

Author:  ofnuts [ Thu Nov 23, 2023 8:28 am ]
Post subject:  Re: Get average color of selection

trandoductin wrote:
What I am confused doesn't gimp_histogram do mean meaning mean? What's the error in logics since I can't see any.
Or if you mean blend based on perception then there's no way for me to do that accurately.


* The "mean" returned by the API is the mean of gamma-corrected values, without any consideration for the "perception". As far as I can tell, applying the gamma as I did yields a more satisfactory "perceptual" value.
* It remains to be seen if the "average perceptual color" is just the color with the "average perceptual values" for red/green/blue channels (but this seems to be true).

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/