You are here

Even image low freq brightness - script

Hi
I need script that will even image's low frequency brightness (remove darker and lighter areas).
It will make easier to turn image into tileale texture.

I wrote simple script, but result isn't satisfacting:
Brightness_Median.scm

; Brightness Median script v1.0 for GIMP program
;
; 12.VIII.2009
; Author: Miran (Miran4@op.pl)
; You can use/modify on GNU license terms
;

; main script
(define (script-fu-bright_median	image 
					in-drawable 
					radius 
					new-layer
					new-background
					)

   (gimp-image-undo-group-start image) ; start undo point

   (if (= (car (gimp-drawable-is-layer in-drawable)) TRUE)
      (begin
         (let*
	    (
	       (name (car (gimp-drawable-get-name in-drawable)))
	       (drawable 0)
	       (correct-layer 0)
	       (fg-color (car (gimp-context-get-foreground))) ; store current foreground color
	       (selection 0)
	    )

	    (if (= (car (gimp-selection-is-empty image)) FALSE)
	       (set! selection (car (gimp-selection-save image))) ; save selection
	    )

	    (if (= new-layer FALSE)
	       (set! drawable in-drawable)
	       (begin ; else
	          (set! drawable (car (gimp-layer-copy in-drawable TRUE))) ; copy layer
		  (gimp-layer-add-alpha drawable) ; add alpha
	          (gimp-image-add-layer image drawable (car(gimp-image-get-layer-position image in-drawable))) ; add the layer to image

		  (if (and (= (car (gimp-selection-is-empty image)) FALSE) (= new-background FALSE)) ; there is selection, keep background is no selected
		     (begin
		        (gimp-selection-invert image) ; invert selection
			(gimp-edit-clear drawable) ; clear selected
		     )
		  )
	       )
	    )
	    (gimp-layer-set-mode drawable HARDLIGHT-MODE) ; set hardlight blend mode
	    (gimp-image-set-active-layer image drawable) ; active the layer

	    ; create brightness correction layer
	    (set! correct-layer (car (gimp-layer-copy in-drawable TRUE))) ; copy layer
	    (gimp-image-add-layer image correct-layer (car(gimp-image-get-layer-position image drawable))) ; add the layer to image
	    (gimp-image-lower-layer image correct-layer) ; move layer down
	    (gimp-selection-none image) ; clear selection
	    (gimp-layer-set-mode correct-layer NORMAL-MODE) ; set normal blend mode
	    (gimp-desaturate correct-layer) ; gray scale
	    (gimp-invert correct-layer) ; negative
	    (plug-in-gauss RUN-NONINTERACTIVE image correct-layer radius radius 1)

	    (if (not (= selection 0)) ; apply effect only in selected area
	       (begin
	          (gimp-selection-load selection) ; load selection
		  (gimp-selection-invert image) ; invert selection

	          (if (and (= new-layer TRUE) (= new-background FALSE))
                     (gimp-edit-clear correct-layer) ; clear selected
		     (begin ; else
		       (gimp-context-set-foreground '(128 128 128)) ; gray - gives no changes in hardlight mode
		       (gimp-edit-fill correct-layer FOREGROUND-FILL) ; fill selected area
		       (gimp-context-set-foreground fg-color) ; restore old color
		     )
		  )

		  (gimp-selection-invert image) ; invert selection - will be same as original
	       )
	    )

	    (set! drawable (car(gimp-image-merge-down image drawable EXPAND-AS-NECESSARY))) ; final apply

	    (if (= new-layer FALSE)
               (gimp-drawable-set-name drawable name) ; set name
	       (gimp-drawable-set-name drawable (string-append name " - Brightness Median")) ; set name
            )
	 )
      )
      (message-box "MISSING ACTIVE LAYER!\n\Please first active layer you want to process.")
   )
 
   (gimp-image-undo-group-end image) ; end undo point
   (gimp-displays-flush) ; show changes
)

; register script
(script-fu-register "script-fu-bright_median"
	"/Filters/Light and Shadow/Brightness Median" 
	"Removes brightness differences from image. Usefull when creating textures for 3D models." 
	"Miran " 
	"Miran" 
	"7.06.2010" 
	"RGB RGBA GRAY GRAYA"
	SF-IMAGE "Image" 0 
	SF-DRAWABLE "Drawable" 0 
	SF-ADJUSTMENT _"Radius" '( 10 1 500 1 10 0 0 )
	SF-TOGGLE _"Separate layer" FALSE
	SF-TOGGLE _"Keep background" FALSE
)

Run script at Filters -> Light and Shadow -> Brightness Median

Any better ideas?

P.S.: Sorry, I'm new at forum, where are uttons for formating post's text? (code, bold, image etc.)
EDIT: OK, I found it

Something like this:

http://registry.gimp.org/node/7385

?

-Rob A>

Rob's Retrobe's highpass I've used a lot when creating certain tileable textures. Give that a shot.

Yes, it's almost that I need, but could it be better.
I working on my plugin, actually i have to say the results are better than I dreamed!
It will be released soon ;)

Subscribe to Comments for "Even image low freq brightness - script"