GIMP/Scripts/SplitPage

From DPWiki
< GIMP‎ | Scripts
Jump to navigation Jump to search
  • Script names:
    • dp-guillotine-all
    • dp-guillotine
  • Author: Katt83
  • Summary: For all open images, or just the current one:
    1. If no guides, insert them based on selection
    2. Guillotine from guides
      • Creates one new image for each section
    3. For each image-part, save as a new png in the same directory
      • 202.png => 202a.png, 202b.png, ...
    4. Close the new images
  • Notes:
    • Non-windows users: Change the top-line's dir-separator to "/"
; Set this to "/" if not on Windows
(define dir-separator "\\")

; Guillotine all open images
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (dp-guillotine-all)
	(let* ( 
		(image-num  (car (gimp-image-list)) )
		(image-ids  (cadr (gimp-image-list)) ) 
		(counter 0)
	      )
		(while (< counter image-num)
			(let* (
          ; Reference into image-ids array
				(image (aref image-ids counter)) 
			      )
				(dp-guillotine image (car (gimp-image-get-active-drawable image)))
				(set! counter (+ counter 1))
        (print (string-append "All=" (number->string counter) " out of " (number->string image-num)))
			)
		)
	)
)

(define (dp-guillotine image drawable)
    (let* ( (orig_full_filename (car (gimp-image-get-filename image)))
            (orig_file_no_suffix (file-basename orig_full_filename))
            (old_filename (car (last(strbreakup orig_full_filename dir-separator))))
            (first_guide (car (gimp-image-find-next-guide image 0)))
          )
(print old_filename)
        
        (if (zero? first_guide)
            (begin
              ; guides-from-selection returns either () or (#t)
              (set! first_guide (dp-guides-from-selection image drawable))
              (set! first_guide (if (null? first_guide) 0 1))
            )
        )
        (if (zero? first_guide)
            (begin (gimp-message (string-append "Set guide or selections on " old_filename " first!!!"))
              (print (string-append "no guides on " old_filename)))
            ; else
            (begin
                ; guillotine, etc
                (let* ( (new_image_list (plug-in-guillotine RUN-NONINTERACTIVE image drawable))
                        (image-num (car new_image_list))
                        (image-ids (cadr new_image_list))
                        (i 0)
                        (modifier "a")
                      )
                  (while (< i image-num)
                    (let* (
                            ; Reference into image-ids array
                            (image (aref image-ids i))
                            (new_filename (string-append orig_file_no_suffix modifier ".png"))
                          )
(print new_filename)
                      (file-png-save-defaults RUN-NONINTERACTIVE
                                              image
                                              (car (gimp-image-get-active-drawable image))  ; drawable
                                              new_filename
                                              (car (last(strbreakup new_filename dir-separator)))) ; filename (no path)
                      (gimp-image-delete image)
                      
                      ; Increment for the next loop
                      (set! i (+ i 1))
                      (set! modifier (if (= (strcmp modifier "") 0)
                                          ; 2nd file, set modifier to a
                                          "a"
                                          ; else, add one to the modifier character
                                          (increment-char modifier)
                                      ))
                    )
                  ) ; end of while
            )
          )
        ) ; end of if
    )
    (gimp-displays-flush)
)

(define (dp-guides-from-selection image drawable)
  (let* (
        (boundries (gimp-selection-bounds image))
        ;; non-empty INT32 TRUE if there is a selection
        (selection (car boundries))
        (x1 (cadr boundries))  ;1041
        (y1 (caddr boundries))  ; 0
        (x2 (cadr (cddr boundries))) ;1881
        (y2 (caddr (cddr boundries))) ;219
        )

    ;; need to check for a selection or we get guides right at edges of the image
    (if (> selection 0)
      (begin
        (gimp-image-undo-group-start image)

        ; No boundaries on image-edges
        (if (> x1 0)
          (gimp-image-add-vguide image x1))
        (if (> y1 0)
          (gimp-image-add-hguide image y1))
        (if (< x2 (car (gimp-image-width image)))
          (gimp-image-add-vguide image x2))
        (if (< y2 (car (gimp-image-height image)))
          (gimp-image-add-hguide image y2))

        (gimp-image-undo-group-end image)
        (gimp-displays-flush)
      )
    )
  )
)

(define (remove-suffix str)
(let* ((str-length (string-length str))
(str-length-without-suffix (- str-length 4)))
 (substring str 0 str-length-without-suffix)))

(define (replace-suffix str old new)
(string-append (remove-suffix str old) new))

; (file-basename (car (gimp-image-get-filename image)))
(define (file-basename orig-name)
    (car (strbreakup orig-name "."))
)

(define (increment-char c)
  (atom->string (integer->char (+ (char->integer (car (string->list c))) 1)))
)


 (script-fu-register      
    "dp-guillotine"
    "_Guillotine image"
    "Guillotines and saves based on existing guides"
    "Katt"
    "Katt83"
    "08/05/2010"
    "*"                ;accepts any image type 
    SF-IMAGE "Image" 0
    SF-DRAWABLE "Drawable" 0
)

(script-fu-menu-register "dp-guillotine"
                         "<Image>/DP")

 (script-fu-register      
    "dp-guillotine-all"
    "Guillotine _All images"
    "Guillotines and saves all open images"
    "Katt"
    "Katt83"
    "08/05/2010"
    ""                ;accepts any image type 
)

(script-fu-menu-register "dp-guillotine-all"
                          "<Image>/DP")