Skip to content

Instantly share code, notes, and snippets.

@endavid
Created December 18, 2019 17:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endavid/fceea065e586052de944ffc57014692c to your computer and use it in GitHub Desktop.
Save endavid/fceea065e586052de944ffc57014692c to your computer and use it in GitHub Desktop.
Multilayered PSD from a list of images using #Gimp
;; Copy to ~/Library/Application\ Support/GIMP/2.10/scripts/
;; Ref. https://stackoverflow.com/a/24164916/1765629
;; Usage example:
;; gimp -i -b '(layers-to-psd (list "Background.jpg" "A.png" "B.png") "compo.psd")' -b '(gimp-quit 0)'
;;
(define (layers-to-psd image-paths psd-path)
(define (add-layers image image-paths)
(when (not (null? image-paths))
(let*
(
(img-path (car image-paths))
(layer (car (gimp-file-load-layer RUN-NONINTERACTIVE image img-path)))
)
;; (display img-path) (newline)
(gimp-image-insert-layer image layer 0 -1)
(add-layers image (cdr image-paths))
)
)
)
(let*
(
(base-path (car image-paths))
(image (car (gimp-file-load RUN-NONINTERACTIVE base-path base-path)))
(drawable (car (gimp-image-get-active-layer image)))
)
;; (display base-path) (newline)
(add-layers image (cdr image-paths))
;; LZW compression (1), MSB to LSB fill order (0)
(file-psd-save RUN-NONINTERACTIVE image drawable psd-path psd-path 1 0)
(gimp-image-delete image)
)
)
@endavid
Copy link
Author

endavid commented Dec 18, 2019

A variation if you want to create your own image with an arbitrary precision:

  (let* 
    (
      (base-path (car image-paths))
      (base-img (car (gimp-file-load RUN-NONINTERACTIVE base-path base-path)))
      (width (car (gimp-image-width base-img)))
      (height (car (gimp-image-height base-img)))
      (image (car (gimp-image-new-with-precision width height RGB PRECISION-U16-GAMMA)))
      (drawable (car (gimp-image-get-active-layer image)))
    )
    (gimp-image-delete base-img)
    (add-layers image image-paths)
    ;; LZW compression (1), MSB to LSB fill order (0)
    (file-psd-save RUN-NONINTERACTIVE image drawable psd-path psd-path 0 0)
    ;; PSD exporter doesn't support 16-bit images, it seems. Use xcf for that:
    ;; (gimp-xcf-save RUN-NONINTERACTIVE image drawable psd-path psd-path)
    (gimp-image-delete image)
  )

(the add-layers function stays the same as above)

@divyanshi00
Copy link

divyanshi00 commented Apr 29, 2022

hey I am trying to generate PSD through the same code, my batch commands are being executing successfully but I am unable to generate PSD as output file. Kindly help.
P.S - I am trying it in windows.

@endavid
Copy link
Author

endavid commented May 24, 2022

Do you get any error messages? I can't help you without any information. Although even if I had the info, I'm not very familiar with GIMP. You better post a question in Stack Overflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment