Gimptalk - Premier Gimp Community: Density Spatter Script - Gimptalk - Premier Gimp Community

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Density Spatter Script

#1 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 721
  • Joined: 05-November 07

Posted 13 April 2008 - 04:36 PM

I had asked a question in this post here about a script to randomly paint dabs based on a specified density map (see examples in that post).

I have cleaned it up, applied some error handling, as well as a bailout threshold (to stop it from going into an infinite loop). The starting point was this script by Charles Cave.

Here is the final script:

; random_density_map.scm
; by Rob Antonishen
; http://ffaat.pointclark.net

; Version 1.0 (20080408)

; Description
;
; Script to draw a specified number of random points with the currently 
; selected brush, using a density mask.
; Will appear in Filters->Map Menu
;

; based on random.scm
; by Charles Cave 
; http://members.optusnet.com.au/~charles57/GIMP

; License:
;
; 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.
;
; The GNU Public License is available at
; http://www.gnu.org/copyleft/gpl.html
(define (script-fu-random-density-map img inLayer inDensityMap inInvertMap inUseAlpha inIterations inMargin inBailout)

  (let* (
        (x1 0)
        (y1 0)
        (ctr 0)
        (x2 (car (gimp-drawable-width inLayer)))
        (y2 (car (gimp-drawable-height inLayer)))
        (xMap (car (gimp-drawable-width inDensityMap)))
        (yMap (car (gimp-drawable-height inDensityMap)))
        (x 0)
        (y 0)
        (map-type 0)
        (get-val 0)
        (test 0)
        (thresh 0)
        (*randompoint* (cons-array 2 'double))
        (drw-width 0)
        (drw-height 0)
		(lc 0)
        )

  ; define drawable area for the algorithm
  (set! x1 (+ x1 inMargin))
  (set! x2 (- x2 inMargin))
  (set! y1 (+ y1 inMargin))
  (set! y2 (- y2 inMargin))  (set! drw-width  (- x2 x1))
  (set! drw-height (- y2 y1))

  ;get map type:  RGB-IMAGE (0), RGBA-IMAGE (1), GRAY-IMAGE (2), GRAYA-IMAGE (3), INDEXED-IMAGE (4), INDEXEDA-IMAGE (5) 
  (set! map-type (car (gimp-drawable-type inDensityMap)))

  ; make sure maps isn't indexed
  (if (or (= map-type INDEXED-IMAGE) (= map-type INDEXEDA-IMAGE))
    (gimp-message "Density Map can not be indexed!") ; error
	(if (or (not (= x2 xMap)) (not (= y2 yMap)))
      (gimp-message "Density Map must be the same size as the active layer!") ; error
	    ;it begins here
		(begin 
		 (gimp-context-push)
		 (gimp-image-undo-group-start img)
		 
		 ; set up progress bar
		 (gimp-progress-set-text _"Rendering Random masked Image")
		 
		 (while (and (< ctr inIterations) (< lc inBailout))
		 
		   ; get a random location
		   (set! x (+ x1 (random drw-width )))
		   (set! y (+ y1 (random drw-height)))

		   ; get the pixel value at that spot
		   (set! get-val (cadr (gimp-drawable-get-pixel inDensityMap x y)))
		   
		   ; pick a number, any number
		   (set! thresh (random 255))

		   (if (and (= inUseAlpha TRUE) (= (car (gimp-drawable-has-alpha inDensityMap)) TRUE))   ; if using alpha
			 (begin
			   (set! test (aref get-val 3))   ; test threshold against alpha ( index 3)
			   (gimp-context-set-foreground (list (aref get-val 0) (aref get-val 1) (aref get-val 2)))  ;set brush colour
			 )
			 (if (or (= map-type GRAY-IMAGE) (= map-type GRAYA-IMAGE))  ;  else if greyscale
			   (set! test (aref get-val 0))  ; test threshold against colour ( index 0)
			   (if (or (= map-type RGB-IMAGE) (= map-type RGBA-IMAGE))  ; else if colour 
				 (set! test (/ (+ (aref get-val 0) (aref get-val 1) (aref get-val 2)) 3)) ; test threshold against average of r g and b
				 (set! test 255) ;  else threshold to full value (always draw)
			   )
			 )
		   )			   
		   (if (= inInvertMap TRUE)  ; reverse the map if invert option
			 (set! test (- 255 test))
		   )
		   (if (< thresh test)    ; compare threshold against random value
			 (begin
			   (set! ctr (+ ctr 1))       ; increment counter
			   (set! lc 0)
			   (aset *randompoint* 0 x)   ; set the paint array
			   (aset *randompoint* 1 y)
			   (gimp-paintbrush-default inLayer 2 *randompoint*) ; paint point
			   (gimp-progress-update (/ ctr inIterations))  ;update progress bar
			 )
		   )
		   (set! lc (+ lc 1))
		 )
		 
		 (if (>= lc inBailout)
		   (gimp-message "Bailout Parameter Exceeded - Aborted!") ; error      
		 )
		 
		 (gimp-progress-end)
		 (gimp-image-undo-group-end img)
		 (gimp-displays-flush)
		 (gimp-context-pop)
		)
    )
    )
  )  
)

(script-fu-register "script-fu-random-density-map"
                    "/Filters/Map/Random Density Map..."
                    "Draw a specified number of random points with the currently selected brush, using a density mask."
                    "Rob Antonishen"
                    "Rob Antonishen"
                    "April 2008"
                    "RGB*, GRAY*"
                    SF-IMAGE       "Image"         0
                    SF-DRAWABLE    "Drawable"      0
                    SF-DRAWABLE    "Density Map"   -1
				    SF-TOGGLE      "Invert Map"    FALSE
					SF-TOGGLE      "Use map alpha channel for Density and draw with map colour"  FALSE
                    SF-ADJUSTMENT  "Number of points to draw"     '(10 1 10000 10 100 0 0) 
                    SF-ADJUSTMENT  "Border Margin (pixels)"       '(0 0 100 1 6 0 0)
                    SF-ADJUSTMENT  "Bailout Threshold"            '(200 100 2000 10 100 0 0)
                    )


It uses the currently selected tool.

The density map can either be a greyscale map, in which case it will draw with the current pen and colour, or a layer with transparency. In that case, it will use the alpha for density and paint using the colour of the layer with transparency. See the examples in the other thread.

-Rob A>
Posted Image
Fantasy Cartography and Mapping by RobA
0

#2 User is offline   Oregonian 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 2,360
  • Joined: 13-November 07

Posted 16 April 2008 - 01:09 AM

I tried your script. Used the default settings. It said it failed ... something about bailout exceeded. It would be helpful if you'd post some instructions with it.
Posted Image
Posted Image
0

#3 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 721
  • Joined: 05-November 07

Posted 30 April 2008 - 01:12 AM

OK-

Create a new image (I'll use 300x300), black canvas. First we'll make a density map. Draw a white circle in the middle and blur it:
Posted Image

This is the density map. The script randomly picks coordinates in the image. Any black areas have no probability of getting a paint blob drawn. white will have a 100% chance, and grey in between.

Create a new white layer to actually apply the filter on. Pick a red circle 7 paintbrush and select the white layer then run the filter.
Posted Image

Here is the result. 200 paint dabs, following the density map.
Posted Image

Here is another sample density map:
Posted Image

and the resultant density filter result. This was a star brush in white on a black background, with 600 paint dabs:
Posted Image

Hope that clarifies the basic usage.

-Rob A>
Posted Image
Fantasy Cartography and Mapping by RobA
0

#4 User is offline   Oregonian 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 2,360
  • Joined: 13-November 07

Posted 25 September 2010 - 05:52 PM

Whoa! Something must have happened and I didn't get back to this. A very belated thank you for those instructions, RobA.

Here's a quicky little image I did using the script.

Posted Image
Posted Image
Posted Image
0

#5 User is offline   lylejk 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 1,405
  • Joined: 19-November 07

Posted 25 September 2010 - 08:42 PM

No support of scaleable brushes unforuntately (ie., set the scale for a brush to 50%, but the script runs the brush size at full size). Anyway, can you add a scale option to this script Rob? I did just save one of my brushes to a smaller size, but it would be cool to no have to do so (have a lot of large brushes that I scale as I need too). :)
0

#6 User is offline   Oregonian 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 2,360
  • Joined: 13-November 07

Posted 25 September 2010 - 10:14 PM

lylejk said:

No support of scaleable brushes unforuntately (ie., set the scale for a brush to 50%, but the script runs the brush size at full size). Anyway, can you add a scale option to this script Rob?
I second that request. :mrgreen:
Posted Image
Posted Image
0

#7 User is offline   RobA 

  • Member
  • PipPip
  • Group: Members
  • Posts: 721
  • Joined: 05-November 07

Posted 25 September 2010 - 10:59 PM

lylejk said:

No support of scaleable brushes unforuntately (ie., set the scale for a brush to 50%, but the script runs the brush size at full size). Anyway, can you add a scale option to this script Rob? I did just save one of my brushes to a smaller size, but it would be cool to no have to do so (have a lot of large brushes that I scale as I need too). :)


Love to but it can't be done in Scheme as far as I can figure out.

-Rob A>
Posted Image
Fantasy Cartography and Mapping by RobA
0

#8 User is offline   lylejk 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 1,405
  • Joined: 19-November 07

Posted 25 September 2010 - 11:23 PM

That's cool Rob. Only affects gih brushes of course (can copy and past for gbrs). Had some fun with this cool Script-fu at GIMPChat by the way. :)
0

#9 User is offline   Oregonian 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 2,360
  • Joined: 13-November 07

Posted 25 September 2010 - 11:52 PM

Here's a path to our playing. :mrgreen:

http://www.gimpchat.....php?f=11&t=530
Posted Image
Posted Image
0

#10 User is offline   lylejk 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 1,405
  • Joined: 19-November 07

Posted 26 September 2010 - 12:59 AM

Here's one that i just did using my Arcimbolder GIH. Definitely a cool Script-fu. :)

Posted Image

Uploaded with ImageShack.us
0

#11 User is offline   lylejk 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 1,405
  • Joined: 19-November 07

Posted 26 September 2010 - 01:21 AM

Just as a side note, I wonder if you are going to officially release this one on the Registry? I saw your link to your grid version. I happen to like both so I did (for my own personal use) edit the grid version so that it can co-exist with the full random version on my PC. I really do think it's worthy Rob of it. Really have enjoyed playing with your Script-fus over the past few years Rob. I actually created the Arcimbodo brush using your Rotating Brush assistant Script-fu. :)
0

#12 User is offline   Rod_Blake 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 03-July 10

Posted 26 September 2010 - 01:23 AM

I keep getting an integer error. =\
Posted Image
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic