Permalink
Browse files

Sharing a scheme experience in gimp

  • Loading branch information...
1 parent f169e49 commit b9840a22ecaf5cc0d5993d340e2a57579f6ed465 @gautamc committed Aug 18, 2010
Showing with 187 additions and 0 deletions.
  1. +31 −0 _posts/2010-08-17-extracting-layers-from-psd-gimp.textile
  2. +151 −0 code/sg-save-all-layers.scm
  3. +5 −0 index.html
@@ -0,0 +1,31 @@
+---
+layout: post
+title: gimp-image-get-layers - Using the Gimp to extract layers from a psd
+---
+
+h3. {{ page.title }}
+
+p(meta). 17 Aug 2010
+
+<div class="post_payload">
+
+Time and again I've seen that programming on stuff for fun ends up teaching me intresting new things.
+
+Up until now, I had been always hacking around with lisp only in the context of the emacs editor (since emacs has lisp interpreter that allows us to customize emacs).
+Today, I figured I might as well put together some scheme code see how it feels. Since the Gimp is pretty much the "scheme supporting tool" that I have used most, I started looking for an API. Not suprisingly the API was not to difficult to find. It was right there in the Gimp.<a target="_blank" href="http://lh4.ggpht.com/_oYtjzHEnfk4/TGw-gf2CasI/AAAAAAAAAF4/qDW_vqd4808/s800/gimp_proc_browser.png">(File -> Xtns -> Procedure Browser)</a>. In fact, the gimp also provide a simple rep(read/execute/print) console where one can type in scheme lines of code and execute them (very similar to ruby's irb)
+
+In the process of getting to know the gimp's I saw that it was pretty easy to extract out layers from an image. This function is understandably named: gimp-image-get-layers and takes an image object as its argument.
+
+The coolest part is that function can be used to extract out the layers of a PSD. Hence, if I had a psd and wanted to pull out all the images in it while converting it into an CSS/XHTML file, I can use the gimp to do.
+
+After some hacking around and going through the existing scripts in the gimp registry I discovered this <a href="/code/sg-save-all-layers.scm">script</a> for exactly the same task.
+
+<pre>
+$ cd ~/.gimp-x.y/scripts
+$ wget http://blog.trnscofi.in/code/sg-save-all-layers.scm .
+$ gimp &
+</pre>
+
+You will see the "File->Save all layers" menu item after you open a layered image!
+
+</div>
View
@@ -0,0 +1,151 @@
+; 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.
+
+;; Command is installed in "File->Save all layers..."
+;;
+;; A template string should be provided which fits the form: prefix~~~~.ext
+;; where prefix is a character string (optionally null).
+;; ~~~~ represents the digits of the frame number, one ~ per digit
+;; ext is the filename extension (which also specifies the format)
+;; the tildas are optional and four digits will be assumed if omitted.
+;; an extension of .png is assumed if one is not provided
+;; the period is significant, if PNG is not to be assumed
+;;
+;; A checkbox provides the option of using the layernames for the
+;; filenames. The extension given in the template is used to determine
+;; the file type. Animation settings (delay and frame disposal) are not
+;; included as part of the filename.
+;;
+;; When saving to GIF files, the GIMP's default values are used to
+;; convert to INDEXED mode (255 color palette, no dithering).
+;; Note: this is done on a layer-by-layer basis, so more colors may result
+;; than if the entire image were converted to INDEXED before saving.
+
+
+(define (sg-save-all-layers orig-image drawable
+ template
+ rename?)
+ (define (get-all-layers img)
+ (let* (
+ (all-layers (gimp-image-get-layers img))
+ (i (car all-layers))
+ (bottom-to-top ())
+ )
+ (set! all-layers (cadr all-layers))
+ (while (> i 0)
+ (set! bottom-to-top (append bottom-to-top (cons (aref all-layers (- i 1)) '())))
+ (set! i (- i 1))
+ )
+ bottom-to-top
+ )
+ )
+ (define (save-layer orig-image layer name)
+ (let* (
+ (image 0)
+ (buffer "")
+ )
+ (set! buffer (car (gimp-edit-named-copy layer "temp-copy")))
+ (set! image (car (gimp-edit-named-paste-as-new buffer)))
+ (when (and (not (= (car (gimp-image-base-type image)) INDEXED))
+ (string-ci=? (car (last (strbreakup name "."))) "gif"))
+ (gimp-image-convert-indexed image
+ NO-DITHER
+ MAKE-PALETTE
+ 255
+ FALSE
+ FALSE
+ "")
+ )
+ (gimp-file-save RUN-NONINTERACTIVE image (car (gimp-image-get-active-layer image)) name name)
+ (gimp-buffer-delete buffer)
+ (gimp-image-delete image)
+ )
+ )
+ (let* (
+ (layers nil)
+ (fullname "")
+ (basename "")
+ (layername "")
+ (format "")
+ (layerpos 1)
+ (framenum "")
+ (settings "")
+ (default-extension "png")
+ (extension "png")
+ (orig-selection 0)
+ )
+ (gimp-image-undo-disable orig-image)
+ (set! orig-selection (car (gimp-selection-save orig-image)))
+ (gimp-selection-none orig-image)
+
+ (set! extension (strbreakup template "."))
+ (set! extension (if (> (length extension) 1)
+ (car (last extension))
+ default-extension))
+ (when (= (string-length extension) 0)
+ (set! default-extension "png"))
+ (when (= rename? TRUE)
+ (set! format (strbreakup template "~"))
+ (if (> (length format) 1)
+ (begin
+ (set! basename (car format))
+ (set! format (cdr format))
+ (set! format (cons "" (butlast format)))
+ (set! format (string-append "0" (unbreakupstr format "0")))
+ )
+ (begin
+ (set! basename (car (strbreakup template ".")))
+ (set! format "0000")
+ )
+ )
+ )
+ (set! layers (get-all-layers orig-image))
+ (while (pair? layers)
+ (if (= rename? TRUE)
+ (begin
+ (set! framenum (number->string layerpos))
+ (set! framenum (string-append
+ (substring format 0 (- (string-length format)
+ (string-length framenum))) framenum))
+ (set! fullname (string-append basename framenum))
+ )
+ (begin
+ (set! fullname (car (strbreakup
+ (car (gimp-drawable-get-name (car layers))) "(")))
+ (gimp-drawable-set-name (car layers) fullname)
+ (set! fullname (car (gimp-drawable-get-name (car layers))))
+ )
+ )
+ (set! fullname (string-append fullname "." extension))
+ (save-layer orig-image (car layers) fullname)
+ (set! layers (cdr layers))
+ (set! layerpos (+ layerpos 1))
+ )
+ (gimp-selection-load orig-selection)
+ (gimp-image-remove-channel orig-image orig-selection)
+ (gimp-image-undo-enable orig-image)
+ )
+ )
+
+(script-fu-register "sg-save-all-layers"
+ "Save all layers..."
+ "Save each layer to a file."
+ "Saul Goode"
+ "Saul Goode"
+ "11/16/2008"
+ ""
+ SF-IMAGE "Image" 0
+ SF-DRAWABLE "Drawable" 0
+ SF-STRING "Name Template (~ replaced by layer position)" "frame_~~~~.png"
+ SF-TOGGLE "Rename (ex: 'frame__0001')" TRUE
+ )
+
+(script-fu-menu-register "sg-save-all-layers"
+ "<Image>/File/")
View
@@ -36,6 +36,11 @@
<h3>Blog Posts</h3>
<ul class="posts">
<li>
+ <span>17 Aug 2010</span> &raquo;
+ <a href="2010/08/17/extracting-layers-from-psd-gimp.html">
+ gimp-image-get-layers - Using the Gimp to extract layers from a psd
+ </a>
+ <br />
<span>16 Aug 2010</span> &raquo;
<a href="/2010/08/16/genrating-strong-passwords.html">Genrating strong passwords on Debian lenny</a>
<br />

0 comments on commit b9840a2

Please sign in to comment.