Kyle R . Burton on Wed, 7 May 2003 11:58:04 -0400


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Gimp Users?


> Of course, JPEG's are RGB.  Where would the layers have come from?  What 
> is the command to "flatten" an image in a script?

I wrote the script to perform the following steps on an image:

- duplicate the image:
  (let* (...
        (the-copy (car (gimp-channel-ops-duplicate in-the-image)))
        ...
        )
    ...
    )
- flatten the image if necessary;
  (if (< 1 (car (gimp-image-get-layers the-copy)))
        (gimp-image-merge-visible-layers the-copy 1))
  gimp-image-merge-visible-layers fails unless there are
  multiple layers in the image.
- scale the copy to the desired width
- create a 'final' image that is the desired size plus the border size
- bucket fill the 'final' image
- copy the scaled image to the clipboard
- paste it into the final image (this centers it as well)
- destroy the copy

On some of the screenshots I'm adding other visual elements (like 
highlighting to point out elements on the page that the documentation is
referring to).  I'm doing this using layers in Gimp so I can save my
intermediate work in case I need to go back and change (fix) something.  
Also, for better final image resolution, I'm working with a larger image 
than will be included into the documentation.

The script was just an attempt to automate the final steps of flattening,
scaling and adding the border.

Thanks to everyone for the help and discussion, I did manage to figure out
what the issue with saving to JPEG was - the drawable (layer) that was being
passed to file-jpeg-save wasn't associated with the image the code was 
attempting to save, once I changed the code to pull the active drable 
(gimp-image-active-drawable) from the image itself it saved just fine.

The final working script is below in case anyone is interested in it.


Thanks again,


Kyle R. Burton





; Copyright (C) 2003 Kyle R. Burton <mortis@voicenet.com>
;
; 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.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

; (print-to-string '(a b c) "                  ")
; (strbreakup "/foo/bar") => '("" "foo" "bar")


;
; This bucket-fills the specified layer with the given bg color.
; It first saves off and then restores the bgcolor so the user's 
; application state remains unmodified.
;
(define (sf-hms-bucket-fill-drawable in-drawable in-color)
  (let* ((save-bgcolor (car (gimp-palette-get-background))))
    (gimp-palette-set-background in-color)
    (gimp-bucket-fill in-drawable 1 0 100 255 0 0 0)
    (gimp-palette-set-background save-bgcolor)))

;
; Simple script that automates the process of collapsing layers, scaling the
; image and adding a solid color border for screenshots.  This was written
; to be used for the screenshots used in our DocBook documentation.
;
; The ability to specify a width is especially important for publishing to 
; PS and PDF formats.
;
(define (sf-hms-standardize-screenshot in-the-image in-the-drawable in-interactive in-absolute-width in-border-width in-bgcolor)
  (let* ((image-width        (car (gimp-image-width in-the-image)))
         (image-height       (car (gimp-image-height in-the-image)))
         (scale-width        (- in-absolute-width (* 2 in-border-width)))
         (scale-aspect-ratio (/ scale-width image-width))
         (scale-height       (* scale-aspect-ratio image-height))
         (aspect-ratio       (/ in-absolute-width image-width))
         (the-copy           (car (gimp-channel-ops-duplicate in-the-image)))
         (final-height       (* aspect-ratio image-height))
         (final-image        (car (gimp-image-new in-absolute-width final-height RGB)))
         (final-bg-layer     (car (gimp-layer-new final-image in-absolute-width final-height RGB_IMAGE "bg" 100 NORMAL))))
    
    ; disabling undo save memory and performance...
    (gimp-image-undo-disable the-copy)
    (gimp-image-undo-disable final-image)

    ; if the copied image has multiple layers, collapse them
    (if (< 1 (car (gimp-image-get-layers the-copy)))
        (gimp-image-merge-visible-layers the-copy 1))

    ; scale the copy so that with the border it will fit 
    ; into the specified width
    (gimp-image-scale the-copy scale-width scale-height)
    
    (gimp-image-add-layer final-image final-bg-layer 0)
    (gimp-layer-set-visible final-bg-layer 1)
    
    (sf-hms-bucket-fill-drawable final-bg-layer in-bgcolor)
    
    (gimp-edit-copy (car (gimp-image-active-drawable the-copy)))
    (gimp-edit-paste final-bg-layer 0)
    (gimp-image-flatten final-image)
    (gimp-image-delete the-copy)
    (gimp-image-undo-enable final-image)
    (if in-interactive (gimp-display-new final-image))
    ; return the final image...
    (list final-image final-bg-layer)
    )
  )


(script-fu-register 
 "sf-hms-standardize-screenshot"
 _"<Image>/Script-Fu/HMS/Standardize Screenshot"
 "Standardize Screenshot"
 "Kyle R. Burton <mortis@voicenet.com>"
 "Kyle R. Burton"
 "2003"
 ""
 SF-IMAGE    "Image"        0
 SF-DRAWABLE "Layer"        0
 SF-TOGGLE   "Make Viewable" TRUE
 SF-VALUE    "Final Width"  "430"
 SF-VALUE    "Border Width" "2"
 SF-COLOR    "Border Color" '(0 0 0)
)

(define (sf-hms-prefix-filename pfx name ext)
  (let* ((parts (strbreakup name "/"))
         (path (reverse (cdr (reverse parts))))
         (file (car (last parts)))
         (file-parts (strbreakup file "."))
         (file-name (car file-parts))
         (file-ext (cadr file-parts))
         (rv "")
         )
    (while path
           (set! rv (string-append rv (car path) "/" ))
           (set! path (cdr path)))
    (string-append rv pfx file-name "." ext)))

(define (sf-hms-write-jpeg-file in-image in-target in-filename)
  (let* ((filename (sf-hms-prefix-filename in-target in-filename "jpg"))
         (drawable (car (gimp-image-active-drawable in-image)))
         (quality 0.95)
         (smoothing 0.0))
      (file-jpeg-save
       1
       in-image
       drawable
       filename
       filename
       quality
       0.0
       1
       1
       ""
       0
       1
       0
       0
       )
  ))

(define (sf-hms-write-gif-file in-image in-target in-filename)
  (let* ((filename (sf-hms-prefix-filename in-target in-filename "gif"))
         (drawable (car (gimp-image-active-drawable in-image))))
    (gimp-convert-indexed in-image 0 0 255 0 0 "")
    (file-gif-save
     1
     in-image
     drawable
     filename
     filename
     0 ; interlace
     0 ; loop
     0 ; default_delay
     0 ; default_dispose
     )
  ))

(define (sf-hms-create-both in-the-image in-the-drawable)
  (let* ((filename (car (gimp-image-get-filename in-the-image)))
         (image)
         )
    ; this one is for publishing to print
    (set! image (car (sf-hms-standardize-screenshot in-the-image in-the-drawable 0 430 2 '(0 0 0))))
    (gimp-image-undo-disable image)
    (sf-hms-write-jpeg-file image "print-" filename)
    (sf-hms-write-gif-file  image "print-" filename)
    (gimp-image-delete image)

    ; this one is for html output
    (set! image (car (sf-hms-standardize-screenshot in-the-image in-the-drawable 0 800 2 '(0 0 0))))
    (gimp-image-undo-disable image)
    (sf-hms-write-jpeg-file image "html-" filename)
    (sf-hms-write-gif-file  image "html-" filename)
    (gimp-image-delete image)

    (gimp-message "wrote 430 width and 800 width resolutions")
    )
  )

(script-fu-register 
 "sf-hms-create-both"
 _"<Image>/Script-Fu/HMS/Create Both"
 "Standardize Screenshot, both html [800w] and print [430w] sizings."
 "Kyle R. Burton <mortis@voicenet.com>"
 "Kyle R. Burton"
 "2003"
 ""
 SF-IMAGE    "Image"        0
 SF-DRAWABLE "Layer"        0
)


(define (sf-hms-add-shade-layer in-image in-drawable)
  (let* ((shade-layer (car (gimp-layer-new in-image
                                      (car (gimp-image-width in-image))
                                      (car (gimp-image-height in-image))
                                      RGB_IMAGE
                                      "shade"
                                      30
                                      NORMAL))))
    (gimp-image-add-layer in-image shade-layer 0)
    ;(gimp-layer-set-visible shade-layer 1)
    (sf-hms-bucket-fill-drawable shade-layer '(0 0 0)))
  )

(script-fu-register 
 "sf-hms-add-shade-layer"
 _"<Image>/Script-Fu/HMS/Add Shade"
 "Adds a shade layer (filled black with 30% opactiy)"
 "Kyle R. Burton <mortis@voicenet.com>"
 "Kyle R. Burton"
 "2003"
 ""
 SF-IMAGE    "Image"        0
 SF-DRAWABLE "Layer"        0
)

-- 

------------------------------------------------------------------------------
Wisdom and Compassion are inseparable.
        -- Christmas Humphreys
mortis@voicenet.com                            http://www.voicenet.com/~mortis
------------------------------------------------------------------------------
_________________________________________________________________________
Philadelphia Linux Users Group        --       http://www.phillylinux.org
Announcements - http://lists.netisland.net/mailman/listinfo/plug-announce
General Discussion  --   http://lists.netisland.net/mailman/listinfo/plug