Can't believe this thread is 10 years old. I have updated this script to work with GIMP 2.10.20 ( I wasted far too much time hunting an error while in 2.10.14, which disappeared after updating ).
This largely involved replacing deprecated calls with their replacements, figuring out what had to be changed in the call, as well as dealing with the changes to the inputs of the histogram / threshold commands (0..1 instead of 0..255 range)
I have also added an option to use the median, rather than the mean, presented through the UI, though results may be unexpected depending on selected content.
Right now the option to check for this option is in one of the inner loops. The original code is very efficient, meaning that inner loop doesn't run as often as one would think. Nevertheless, if performance is a concern, this check could be moved outside and the inner code duplicated (with appropriate adjustment) instead.
Note: I didn't have much knowledge of script-fu when I began this, so if there's egregious mistakes, please do aid in making it better.
As apparently one can't upload scripts here, and off-site URLs are too spammy for new users, please accepted the following code block:
; 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 2 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.
; This script creates a new layer on which contiguous regions of the
; selection are filled with the mean (average) value of that region.
;
; Special thanks to Rob Antonishen for providing optimizations and
; making the operation UNDO-able
(define (script-fu-sg-meanmedian-fill orig-image drawable meanmedian)
(let* (
(width (car (gimp-image-width orig-image)))
(height (car (gimp-image-height orig-image)))
(orig-selection 0)
(layer 0)
(raw-selection 0)
(selection 0)
(x 0)
(y 0)
(bounds '())
(orig-drawable drawable)
(image (car (gimp-image-duplicate orig-image)))
(drawable (car (gimp-image-get-active-layer image)))
(buffer "sgmfbuffer")
(selection-drawable 0)
)
(gimp-image-undo-group-start orig-image)
(gimp-image-undo-disable image)
(gimp-context-push)
(set! raw-selection (car (gimp-selection-save image)))
(set! orig-selection (car (gimp-selection-save orig-image)))
(set! selection (car (gimp-channel-copy raw-selection)))
(gimp-image-insert-channel image selection 0 -1)
(gimp-selection-none image)
(gimp-drawable-threshold selection 0 0.5 1)
(gimp-image-set-active-layer image drawable)
(set! layer (car (gimp-layer-new-from-drawable drawable image)))
(gimp-image-insert-layer image layer 0 -1)
(gimp-layer-add-alpha layer)
(gimp-drawable-fill layer FILL-TRANSPARENT)
(gimp-image-select-item image CHANNEL-OP-REPLACE raw-selection)
(set! selection-drawable (car (gimp-image-get-selection image)))
(while (< y height)
(while (< x width)
(if (> (car (gimp-selection-value image x y)) 127)
(begin
(gimp-fuzzy-select selection x y 127 CHANNEL-OP-INTERSECT FALSE FALSE 0 FALSE)
(if (= meanmedian 0)
(begin
(gimp-context-set-foreground
(list (car (gimp-drawable-histogram drawable HISTOGRAM-RED 0 1))
(car (gimp-drawable-histogram drawable HISTOGRAM-GREEN 0 1))
(car (gimp-drawable-histogram drawable HISTOGRAM-BLUE 0 1)))))
(begin
(gimp-context-set-foreground
(list (caddr (gimp-drawable-histogram drawable HISTOGRAM-RED 0 1))
(caddr (gimp-drawable-histogram drawable HISTOGRAM-GREEN 0 1))
(caddr (gimp-drawable-histogram drawable HISTOGRAM-BLUE 0 1)))))
)
(gimp-edit-fill layer FILL-FOREGROUND)
(gimp-channel-combine-masks selection selection-drawable CHANNEL-OP-SUBTRACT 0 0)
(gimp-image-select-item image CHANNEL-OP-REPLACE selection)
(gimp-image-select-rectangle image CHANNEL-OP-INTERSECT x y width 1)
(if (= (car (set! bounds (gimp-selection-bounds image))) TRUE)
(set! x (cadr bounds))
(set! x width))
(gimp-image-select-item image CHANNEL-OP-REPLACE selection)
(if (= (car (set! bounds (gimp-selection-bounds image))) TRUE)
(begin
(set! width (cadddr bounds))
(set! y (max y (caddr bounds)))
(set! height (cadr (cdddr bounds))))
(begin
(set! x width))))
(begin
(set! x 0)
(gimp-image-select-rectangle image CHANNEL-OP-INTERSECT x y width 1)
(if (= (car (set! bounds (gimp-selection-bounds image))) TRUE)
(set! x (cadr bounds))
(set! x width))
(gimp-image-select-item image CHANNEL-OP-REPLACE selection))
)
(gimp-progress-pulse))
(if (= (car (set! bounds (gimp-selection-bounds image))) TRUE)
(begin
(set! x (cadr bounds))
(set! width (cadddr bounds))
(set! y (max (+ y 1) (caddr bounds)))
(set! height (cadr (cdddr bounds))))
(begin
(set! x width)
(set! y height)))
)
(gimp-image-select-item image CHANNEL-OP-REPLACE raw-selection)
(gimp-image-remove-channel image selection)
(gimp-image-remove-channel image raw-selection)
(set! buffer (car (gimp-edit-named-copy layer buffer)))
(set! layer (car (gimp-layer-new-from-drawable orig-drawable orig-image)))
(gimp-image-insert-layer orig-image layer 0 -1)
(gimp-layer-add-alpha layer)
(gimp-drawable-fill layer FILL-TRANSPARENT)
(gimp-floating-sel-anchor (car (gimp-edit-named-paste layer buffer FALSE)))
(gimp-displays-flush)
(gimp-context-push)
(gimp-progress-end)
(gimp-image-select-item orig-image CHANNEL-OP-REPLACE orig-selection)
(gimp-image-remove-channel orig-image orig-selection)
(gimp-image-undo-enable image)
(gimp-image-undo-group-end orig-image)
(gimp-image-delete image)
(gimp-image-set-active-layer orig-image layer)))
(script-fu-register "script-fu-sg-meanmedian-fill"
"Mean/Median Fill"
"Fill selections with the mean or median of the selected regions"
"Saul Goode, Rob Antonishen, pw"
"Saul Goode"
"4/21/2010 / 09/10/2020"
"RGB*"
SF-IMAGE "Image" 0
SF-DRAWABLE "Drawable" 0
SF-OPTION "Fill with" '("Mean" "Median")
)
(script-fu-menu-register "script-fu-sg-meanmedian-fill"
"<Image>/Filters/Misc"
)