Gimptalk - Premier Gimp Community: Save layers as separate png's? - Gimptalk - Premier Gimp Community

Jump to content

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

Save layers as separate png's?

#1 User is offline   rylleman 

  • Member
  • PipPip
  • Group: Members
  • Posts: 35
  • Joined: 13-April 07

Posted 17 November 2008 - 11:28 PM

Anyone know of a script that saves out all layers to separate png images?
Preferably naming them same as layer name.
0

#2 User is offline   saulgoode 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 5,324
  • Joined: 22-August 05

Posted 17 November 2008 - 11:51 PM

http://flashingtwelv...anim-layers.scm
Everybody makes their own fun. If you don't make it yourself it's not fun, it's entertainment.
0

#3 User is offline   rylleman 

  • Member
  • PipPip
  • Group: Members
  • Posts: 35
  • Joined: 13-April 07

Posted 18 November 2008 - 09:38 AM

Thank you.
But for some strange reason that script rendered out all frames as separate images but with the layer and all layers below it visible...
0

#4 User is offline   saulgoode 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 5,324
  • Joined: 22-August 05

Posted 18 November 2008 - 10:49 AM

rylleman said:

Thank you.
But for some strange reason that script rendered out all frames as separate images but with the layer and all layers below it visible...


Oops! That script was intended more for animated GIFs (and performed an "unoptimize" before saving). Try the script at http://flashingtwelv...-all-layers.scm
Everybody makes their own fun. If you don't make it yourself it's not fun, it's entertainment.
0

#5 User is offline   rylleman 

  • Member
  • PipPip
  • Group: Members
  • Posts: 35
  • Joined: 13-April 07

Posted 18 November 2008 - 11:11 AM

Thank you!
This script did exactly what I wanted.
0

#6 User is offline   Mikahl 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 20-January 09

Posted 20 January 2009 - 05:28 PM

Thanks for the script.

Forgive my newbness/bumping this thread - but is there a way of specifying a different directory? Whenever
I try and put the directory in the output filename, it says it can't find it. But I'm sure the directory
is correct.
0

#7 User is offline   PhotoComix 

  • GT Senior Moderator
  • Group: Senior Moderators
  • Posts: 11,288
  • Joined: 13-June 05

Posted 20 January 2009 - 06:29 PM

nothing to forgive on the contrary much better bump a trhead then open a clone.

Now do you notice are 2 different links posted for 2 different scripts?
To which you refer?

Ahhh...you are using Windows ? (Often Windows paths needs to be written in a slightly different way)
0

#8 User is offline   saulgoode 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 5,324
  • Joined: 22-August 05

Posted 21 January 2009 - 11:15 AM

Mikahl said:

Forgive my newbness/bumping this thread - but is there a way of specifying a different directory? Whenever I try and put the directory in the output filename, it says it can't find it. But I'm sure the directory is correct.


Try using forward slashes ("/") instead of backslashes ("\") in the directory path you enter.
Everybody makes their own fun. If you don't make it yourself it's not fun, it's entertainment.
0

#9 User is offline   Mikahl 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 20-January 09

Posted 21 January 2009 - 05:27 PM

Thanks for your replies. I spoke too soon.

I'm on OS X, which is based on OpenBSD, and uses "/". It turns out I just have to use the full-path ( duh ). I thought I could start
relative to only my home directory, but it only recognizes absolute paths :P
0

#10 User is offline   rylleman 

  • Member
  • PipPip
  • Group: Members
  • Posts: 35
  • Joined: 13-April 07

Posted 11 February 2009 - 10:19 PM

I'm more than pleased with this script, using it all the time.
Now one thing that would be even more helpful would be if one could choose to export only visible layers. Would this be easy to implement in this script? If so, could anyone give me some pointers as to where to start?
0

#11 User is offline   saulgoode 

  • Retired Staff
  • PipPipPip
  • Group: Retired Staff
  • Posts: 5,324
  • Joined: 22-August 05

Posted 12 February 2009 - 12:23 AM

Replace the function 'get-all-layers' with the following definition. And replace the call to 'get-all-layers' with a call to 'get-visible-layers'.

For extra credit, modify the script to add it as a checkbox option to the dialog.

(define (get-visible-layers image)
  (let* (
      (all-layers (gimp-image-get-layers image))
      (i (car all-layers))
      (viewable '())
      )
    (set! all-layers (cadr all-layers))
    (while (> i 0)
      (set! i (- i 1))
      (if (= (car (gimp-drawable-get-visible (vector-ref all-layers i))) TRUE)
        (set! viewable (cons (vector-ref all-layers i) viewable))
        )
      )
    viewable
    )
  )

Everybody makes their own fun. If you don't make it yourself it's not fun, it's entertainment.
0

#12 User is offline   rylleman 

  • Member
  • PipPip
  • Group: Members
  • Posts: 35
  • Joined: 13-April 07

Posted 14 February 2009 - 11:54 AM

Thank you!
That's excellent, worked very well!
I'll get back to this thread when I've got that tickbox working, right now I just use two different versions of the script.
0

#13 User is offline   EgorZindy 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 30-April 09

Posted 30 April 2009 - 10:02 AM

Thanks for this script, this is exactly what I needed!

I've added a "save only visible layers" checkbox, if anyone's interested. Not sure I did this right though, so anything wrong with my patch, please share!

Cheers,
Egor

; 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?
                                    save-only-visible-layers)
  (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 (get-visible-layers image)
    (let* (
      (all-layers (gimp-image-get-layers image))
      (i (car all-layers))
      (viewable '())
      )
    (set! all-layers (cadr all-layers))
    (while (> i 0)
      (set! i (- i 1))
      (if (= (car (gimp-drawable-get-visible (vector-ref all-layers i))) TRUE)
        (set! viewable (cons (vector-ref all-layers i) viewable))
        )
      )
    viewable
    )
  )

  (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")
          )
        )
      )
    (if (= save-only-visible-layers TRUE)
      (set! layers (get-visible-layers orig-image))
      (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
 SF-TOGGLE     _"Save only visible layers"    TRUE
 )

(script-fu-menu-register "sg-save-all-layers"
                         "/File/")


0

#14 User is offline   PhotoComix 

  • GT Senior Moderator
  • Group: Senior Moderators
  • Posts: 11,288
  • Joined: 13-June 05

Posted 30 April 2009 - 03:20 PM

nice idea i will try
0

#15 User is offline   rodneyxiii 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 07-October 09

Posted 07 October 2009 - 06:24 PM

I keep getting an error when I try to use the sg-save-all-layers.scm. It works fine when I rename all of the layers as sequential numbers but when I try to keep the layer name as the output file name I get an error. I think it's because the layer name has pipes and spaces in it. Is there a way to fix this by either skipping invalid layer names and writing to a log file or using a different command to create the output file name? I'm not familiar with Script-Fu so any help would be appreciated.
0

#16 User is offline   PhotoComix 

  • GT Senior Moderator
  • Group: Senior Moderators
  • Posts: 11,288
  • Joined: 13-June 05

Posted 07 October 2009 - 07:57 PM

without renaming every layer later on just use in the beginning names with no spaces will not work ? (AS instead then " Line Art Layer" "Line_Art_Layer" or simply " LineArtLayer"

Or in your locale layers names are generated automatically with blanc spaces in ?
0

#17 User is offline   PhotoComix 

  • GT Senior Moderator
  • Group: Senior Moderators
  • Posts: 11,288
  • Joined: 13-June 05

Posted 07 October 2009 - 08:08 PM

OOOPSS forgot my comment ...yes layer names generated automatically contain often blanc spaces
as duplicating the bg layer "background copy"
0

#18 User is offline   rodneyxiii 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 07-October 09

Posted 07 October 2009 - 08:47 PM

PhotoComix said:

without renaming every layer later on just use in the beginning names with no spaces will not work ? (AS instead then " Line Art Layer" "Line_Art_Layer" or simply " LineArtLayer"

Or in your locale layers names are generated automatically with blanc spaces in ?


The psd was sent to me so i could ask them to not use spaces or special characters but I'm hoping there is a way to account for special characters or blanks or even a different character set.
0

#19 User is offline   PhotoComix 

  • GT Senior Moderator
  • Group: Senior Moderators
  • Posts: 11,288
  • Joined: 13-June 05

Posted 08 October 2009 - 09:29 AM

forget that remark.... i just notice also gimp may automatically create layer names with blank spaces.

solution may be a script to rename all layers in a shot (quite painful and slow rename manually 1 by 1) ...or automatically remove blank spaces

But i am not good in coding i hope other may help with a script to batch rename layers
0

#20 User is offline   Thorsten 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 16-October 11

Posted 16 October 2011 - 09:10 PM

To replace characters in layername that prevent from saving I inserted:

      (set! fullname (string-append fullname "." extension))

      (define (list-replace! l old new) ; FIRST-LEVEL replace
      (while (pair? l)
        (if(equal? (car l) old)(set-car! l new))
            (set! l (cdr l))) l )
      (define (string-replace str old-char new-char) ; old = "\, new = #\$
      (let*     ((l (string->list str)))
        (list-replace! l old-char new-char)
            (list->string l)))
      (set! fullname (string-replace fullname #\space #\-))
      (set! fullname (string-replace fullname #\/ #\-))
      (set! fullname (string-replace fullname #\> #\-))
      (set! fullname (string-replace fullname #\< #\-))

      (save-layer orig-image (car layers) fullname)
  


This replaces: space, /, < and > to -

Works now nice for me on windows :-)
0

Share this topic:


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