Don't like ads? PRO users don't see any ads ;-)
Guest

mark-number-circles.scm

By: a guest on Apr 30th, 2012  |  syntax: Scheme  |  size: 5.48 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ; The GIMP -- an image manipulation program
  2. ; Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3. ;
  4. ; This program is free software: you can redistribute it and/or modify
  5. ; it under the terms of the GNU General Public License as published by
  6. ; the Free Software Foundation, either version 3 of the License, or
  7. ; (at your option) any later version.
  8. ;
  9. ; This program is distributed in the hope that it will be useful,
  10. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ; GNU General Public License for more details.
  13. ;
  14. ; You should have received a copy of the GNU General Public License
  15. ; along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16. ;
  17. ; Mark Number Circles -- GIMP script to make sequencial number along a path
  18. ; Copyright (C) 2011 Silas Silva
  19.  
  20.  
  21. ; This program tries to solve a long-term problem for users of image processing
  22. ; programs.  Sometimes it is necessary to "label" (or "mark") parts of a picture
  23. ; to be referenced in a document.  It is hard and repetitive to draw a circle, a
  24. ; number within it, etc., so I developed this script.
  25. ;
  26. ; To work with it, it is first necessary to draw a path on the image, with the
  27. ; vector/path tool, then call the Mark Number Circles script that will create
  28. ; one mark for node on the path.
  29.  
  30.  
  31. ; TODO: this script does not work well with numbers > 9, because of the circle
  32. ; size.  It should be corrected in a future version.
  33.  
  34. ; Main function.  Asks for two parameters:  the circle radius and the image to
  35. ; work with.
  36. ;(define (mark-number-circles radius image)
  37. (define (mark-number-circles image)
  38.   (let ((vec (car (drop (gimp-path-get-points image
  39.                         ; TODO: it looks really ugly to use too many car and cdr
  40.                         (car (car (cdr (gimp-path-list image))))) 3)))
  41.         (radius 30))
  42.     (make-circles (vector->list vec) radius image)))
  43.  
  44. ; Quick (and dirty?) implementation of the drop function of SRFI 1 extensions to
  45. ; Scheme, not implemented in TinyScheme (the interpreter used by Gimp).  It
  46. ; drops n elements of the list and returns the rest.
  47. (define (drop lis n)
  48.   (cond ((= n 0) lis)
  49.         ((not (pair? lis)) lis)
  50.         (else (drop (cdr lis) (- n 1)))))
  51.  
  52. ; Make all circles.  lis is the list of coordinates.  It is the vector returned
  53. ; by gimp-path-list, but transformed in a list with vector->list.  See
  54. ; mark-number-circles function for detais.  radius is the circle radius and
  55. ; image is the image to work with.
  56. (define (make-circles lis radius image)
  57.   ; The i variable will hold the number to be printed within the circle
  58.   (let ((i 1))
  59.     (while (pair? lis)
  60.            ; Extract x and y information
  61.            (let ((x (car lis))
  62.                  (y (car (cdr lis))))
  63.              (gimp-image-undo-group-start image)
  64.              (make-circle x y radius (number->string i) image))
  65.              (gimp-image-undo-group-end image)
  66.  
  67.            ; Other itens are repetitive for us, so drop it.
  68.            (set! lis (drop lis 9))
  69.            (set! i (+ i 1)))))
  70.  
  71. ; Draw ONE circle.  It receives x and y coordinates for the center of the layer
  72. ; (and therefore, the circle), the radius of the circle, the text that will be
  73. ; printed inside the circle and the image to work with.
  74. (define (make-circle x y radius text image)
  75.   ; Create a new layer and define x0 and y0 the top-left coordinate as to make
  76.   ; it be in the center of x y
  77.   (let ((layer (car (gimp-layer-new image radius radius RGBA-IMAGE "Circle" 100 NORMAL-MODE)))
  78.         (x0       (- x  (/ radius 2)))
  79.         (y0       (- y  (/ radius 2)))
  80.         (fontsize (/ radius 1.75)))
  81.  
  82.     ; Adjust layer position
  83.     (gimp-layer-set-offsets layer x0 y0)
  84.     (gimp-image-add-layer image layer 0)
  85.     (gimp-image-set-active-layer image layer)
  86.  
  87.     ; Make a circular selection
  88.     (gimp-ellipse-select image (+ x0 2) (+ y0 2) (- radius 4) (- radius 4) REPLACE TRUE FALSE 0)
  89.  
  90.     ; Paint selection
  91.     ;(gimp-layer-add-alpha layer)
  92.     (gimp-context-set-background "#ffffff")
  93.     (gimp-edit-fill layer BG-IMAGE-FILL)
  94.     (gimp-context-set-brush "Circle (03)")
  95.     (gimp-edit-stroke layer)
  96.     (gimp-layer-set-name layer text)
  97.  
  98.     ; Creates text
  99.     (let* ((text-layer (car (gimp-text-fontname image layer 0 0
  100.                               text 0 TRUE fontsize PIXELS "Sans Bold")))
  101.            ; Get text width and height
  102.            (width      (car (gimp-drawable-width text-layer)))
  103.            (height     (car (gimp-drawable-height text-layer)))
  104.  
  105.            ; Discover xt and yt the toplevel coordinate to make text be in the
  106.            ; center of the circle.
  107.            (xt         (- x (/ width 2)))
  108.            (yt         (- y (/ height 2))))
  109.  
  110.       ; Position text.
  111.       (gimp-layer-set-offsets text-layer xt yt)
  112.       (gimp-floating-sel-anchor text-layer)
  113.       (gimp-selection-none image)
  114.  
  115.       ; Flush contents.
  116.       (gimp-displays-flush))))
  117.  
  118.  
  119. ; Register the script for GIMP.
  120. (script-fu-register "mark-number-circles"
  121.                     "Mark Number Circles"
  122.                     "Produce \"circles\" to be used as numeric marks"
  123.                     "Silas Silva <silasdb@gmail.com>"
  124.                     "Copyright (C) 2011 Silas Silva"
  125.                     "2011-02-21"
  126.                     ""
  127. ;                    SF-VALUE    "Circle Radius"   "40"
  128.                     SF-IMAGE    "Image"             0
  129.                     )
  130.  
  131. ; Register the script in the menu
  132. (script-fu-menu-register "mark-number-circles" "<Image>/Script-Fu")