It is currently Sun Jun 30, 2024 3:50 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Get average color of selection
PostPosted: Wed Nov 22, 2023 11:49 am  (#1) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3999
Location: Canada
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 73 times

_________________
TinT
Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: Get average color of selection
PostPosted: Wed Nov 22, 2023 12:54 pm  (#2) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3999
Location: Canada
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.

_________________
TinT


Top
 Post subject: Re: Get average color of selection
PostPosted: Wed Nov 22, 2023 2:11 pm  (#3) 
Offline
GimpChat Member
User avatar

Joined: Jan 13, 2011
Posts: 2260
Location: Poland
Have you forgotten?

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


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

_________________
Image

Slava
Ukraini!
Top
 Post subject: Re: Get average color of selection
PostPosted: Wed Nov 22, 2023 3:02 pm  (#4) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3999
Location: Canada
I didn't even know that existed. hehe

_________________
TinT


Top
 Post subject: Re: Get average color of selection
PostPosted: Wed Nov 22, 2023 5:06 pm  (#5) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4752
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 2670 times ]

_________________
Image
Top
 Post subject: Re: Get average color of selection
PostPosted: Wed Nov 22, 2023 6:11 pm  (#6) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3999
Location: Canada
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.

_________________
TinT


Top
 Post subject: Re: Get average color of selection
PostPosted: Thu Nov 23, 2023 8:28 am  (#7) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4752
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).

_________________
Image


Top
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Attachment(s) GEGL Color Light Fusion (12 blend modes for color overlay)

9

No new posts Attachment(s) Background color chanage and typed text color change

8

No new posts Attachment(s) Color does not exactly match color in GIMP 2.10 (SOLVED)

9

No new posts Color changes to last color when I touch tablet pen to tablet. HELP

2

No new posts Can't Alpha to Selection

5



* Login  



Powered by phpBB3 © phpBB Group