Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script "slice using guides" and "png export"
#1
Python 
Hello everyone, first sorry if my English is average (I'm a stupid French).
As part of a 3d project, I need to develop a small script for gimp. I hope I'm in the right place Smile



I would need a script that follows these steps;
- save the file
- merge all layers
- slice using guides
-export the different images created in .png.as it is all I can do is put the layers together and save each image in Gimp format.

I've scoured the web and tried to combine the scripts found:


Code:
(define (script-fu-save-png Image layer)
  (let* ((i (car (gimp-image-list))) 
         (image))
    (while (> i 0) 
      (set! image (vector-ref (cadr (gimp-image-list)) (- i 1)))
      (gimp-file-save RUN-NONINTERACTIVE 
                      image 
                      (car (gimp-image-merge-visible-layers image 0))                      
                      (car (gimp-image-get-filename image)) 
                      (car (gimp-image-get-filename image))) 
      (gimp-image-clean-all image) 
      (set! i (- i 1))))) 



(script-fu-register
   "script-fu-save-png"
   "<Image>/File/Save PNG & Exit"
   "Save PNG file and exit GIMP."
   "kes"
   "kes"
   "October 2017"
   "RGB*, GRAY*, INDEXED*"
   SF-IMAGE         "Image" 0
   SF-DRAWABLE   "Layer" 0
)

As it is all I can do is put the layers together and save each image in Gimp format.

In the state I do the "slice using guides" me myself. 
Then the script gathers the layers and saves each image in Gimp format.

I can't find the command to do the "slice using guides" nor the modification to switch to png.

Any ideas ?

Thank you for reading me Smile
Reply
#2
Merging all layers can be done with gimp-image-flatten (but this also removes any transparency).

The equivalent to the "slice using guides" in the UI is plugin-guillotine

gimp-file-save saves the image in whatever format is given as an extension (so as PNG if the file names ends with .png). You can also for a PNG save with gimp-png-save or file-png-save2. Note that all the save operation to "flat" files (PNG, JPG...) only apply to the  specified layer.

This said, I have the gut feeling that this is the wrong way to solve a problem. If you can explain the original problem we can come up with better solutions, perhaps with scripts that already exist. For instance a script of mine will cut an image in regular "tiles" and save each tile to disk (so this would be your "slice and save") and another one can save/export all the layers (so this would be you "flatten, slice and save").
Reply
#3
thank you for your responsiveness Smile
(10-24-2022, 04:19 PM)Ofnuts Wrote: Merging all layers can be done with gimp-image-flatten (but this also removes any transparency).
Too bad I really need transparency..

(10-24-2022, 04:19 PM)Ofnuts Wrote: The equivalent to the "slice using guides" in the UI is plugin-guillotine
Good thing , it's work !


(Honestly, I don't really understand what I'm doing. I'm not a coder.)

Quote:gimp-file-save saves the image in whatever format is given as an extension (so as PNG if the file names ends with .png). You can also for a PNG save with gimp-png-save or file-png-save2. Note that all the save operation to "flat" files (PNG, JPG...) only apply to the  specified layer.


I tried the different "png export" the only one that does not crash my code is "file-png-save-defaults".
But it gives me a script that runs in a loop and always outputs .gimp files and not .png files

Code:
(define (script-fu-save-png Image layer)
 (let* ((i (car (gimp-image-list)))
        (image))
   (while (> i 0)
     (set! image (vector-ref (cadr (gimp-image-list)) (- i 1)))
     (plug-in-guillotine RUN-NONINTERACTIVE
                     image
                     (car (gimp-image-merge-visible-layers image 1))
                )
                
     (file-png-save-defaults RUN-NONINTERACTIVE
                     image
                
                     (car (gimp-image-merge-visible-layers image 1))                      
                     (car (gimp-image-get-filename image))
                     (car (gimp-image-get-filename image)))

     (gimp-image-clean-all image)
     (set! i (- i 1)))))



(script-fu-register
  "script-fu-save-png"
  "<Image>/File/Save PNG & Exit"
  "Save PNG file and exit GIMP."
  "kes"
  "kes"
  "October 2017"
  "RGB*, GRAY*, INDEXED*"
  SF-IMAGE         "Image" 0
  SF-DRAWABLE   "Layer" 1
)

Quote:This said, I have the gut feeling that this is the wrong way .... layers (so this would be you "flatten, slice and save").
I create textures that are used in several projects. On a large image of 2048*1536 pixels.
The obtimal format for the engine is 512*512px, so I cut the whole thing for each export.

Each project involves small variations / retouching of this image.

Working on the set is very convenient in my case.

But exporting 10 images manually each time is unnecessarily painful.

Your scripts seem rather suitable yes.
I would be interested in seeing this, at least to understand the structure of the code.
If you want to share I take it with joy  Big Grin
Reply
#4
Quote:...Too bad I really need transparency..

Instead of Flatten you could try gimp-layer-new-from-visible and apply "Slice Using Guides" to that new layer.

Then there is a script that will export all those new images sg-save-all.scm you might pick out parts of that.

By hand it goes:

Make layers visible / not visible
With guides in place Image -> slice using guides.
If required close original image
Use File -> Save ALL As to export in required format.

As an animation https://i.imgur.com/JADobE2.mp4

I am no script writer either, so it is up-to-you.

The old Saul Goode script sg-save-all.scm attached. Unzip put in your Gimp user scripts folder.


Attached Files
.zip   sg-save-all.zip (Size: 1.06 KB / Downloads: 14)
Reply
#5
OK, thanks lot !
indeed, this script is almost perfect for me.
I added the "plug-in-guillotine", it works.
The "gimp-image-flatten" also works. But indeed it removes the alpha Confused .

So I'm trying to make the "gimp-layer-new-from-visible" work,
But it's more complex and I can't find how to call it effectively it seems that it needs to be combined with "gimp-image-insert-layer". 
But that's beyond my skills, does anyone have an example of a piece of code where "gimp-layer-new-from-visible" is functional ?

The code with "plug-in-guillotine" ;
Code:
; 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.


(define    (plug-in-guillotine RUN-NONINTERACTIVE
            image
            
    ))            

(define (script-fu-save-all-images inDir
          inSaveType
          inFileName
          inFileNumber)
 (let* (
         (i (car (gimp-image-list)))
         (ii (car (gimp-image-list)))
         (image)
         (newFileName "")
         (saveString "")
         (pathchar (if (equal? (substring gimp-dir 0 1) "/") "/" "\\"))
       )
   (set! saveString
     (cond
       (( equal? inSaveType 0 ) ".png" )
       (( equal? inSaveType 1 ) ".bmp" )
       (( equal? inSaveType 2 ) ".jpg" )
       (( equal? inSaveType 3 ) ".tif" )
     )
   )
   (while (> i 0)
     (set! image (vector-ref (cadr (gimp-image-list)) (- i 1)))
     (set! newFileName (string-append inDir
             pathchar inFileName
             (substring "00000" (string-length
             (number->string (+ inFileNumber i))))
             (number->string (+ inFileNumber i)) saveString))
    

    
            

     (gimp-file-save RUN-NONINTERACTIVE
                     image
                     (car (gimp-image-get-active-layer image))
                     newFileName
                     newFileName
     )
     (gimp-image-clean-all image)
     (set! i (- i 1))
   )
 )
)




(script-fu-register "script-fu-save-all-images"
"<Image>/Save ALL As"
"Save all opened images as ..."
"Lauchlin Wilkinson (& Saul Goode)"
"Lauchlin Wilkinson (& Saul Goode)"
"2014/04/21"
""
SF-DIRNAME    "Save Directory" ""
SF-OPTION     "Save File Type" (list "png" "bmp" "jpg" "tif")
SF-STRING     "Save File Base Name" "IMAGE"
SF-ADJUSTMENT "Save File Start Number" (list 0 0 9000 1 100 0 SF-SPINNER)
)
Reply
#6
(10-25-2022, 09:29 AM)Lamidetlm Wrote: thank you for your responsiveness Smile
(10-24-2022, 04:19 PM)Ofnuts Wrote: Merging all layers can be done with gimp-image-flatten (but this also removes any transparency).
Too bad I really need transparency..

(10-24-2022, 04:19 PM)Ofnuts Wrote: The equivalent to the "slice using guides" in the UI is plugin-guillotine
Good thing , it's work !


(Honestly, I don't really understand what I'm doing. I'm not a coder.)

Quote:gimp-file-save saves the image in whatever format is given as an extension (so as PNG if the file names ends with .png). You can also for a PNG save with gimp-png-save or file-png-save2. Note that all the save operation to "flat" files (PNG, JPG...) only apply to the  specified layer.


I tried the different "png export" the only one that does not crash my code is "file-png-save-defaults".
But it gives me a script that runs in a loop and always outputs .gimp files and not .png files

Code:
(define (script-fu-save-png Image layer)
 (let* ((i (car (gimp-image-list)))
        (image))
   (while (> i 0)
     (set! image (vector-ref (cadr (gimp-image-list)) (- i 1)))
     (plug-in-guillotine RUN-NONINTERACTIVE
                     image
                     (car (gimp-image-merge-visible-layers image 1))
                )
                
     (file-png-save-defaults RUN-NONINTERACTIVE
                     image
                
                     (car (gimp-image-merge-visible-layers image 1))                      
                     (car (gimp-image-get-filename image))
                     (car (gimp-image-get-filename image)))

     (gimp-image-clean-all image)
     (set! i (- i 1)))))



(script-fu-register
  "script-fu-save-png"
  "<Image>/File/Save PNG & Exit"
  "Save PNG file and exit GIMP."
  "kes"
  "kes"
  "October 2017"
  "RGB*, GRAY*, INDEXED*"
  SF-IMAGE         "Image" 0
  SF-DRAWABLE   "Layer" 1
)

Quote:This said, I have the gut feeling that this is the wrong way .... layers (so this would be you "flatten, slice and save").
I create textures that are used in several projects. On a large image of 2048*1536 pixels.
The obtimal format for the engine is 512*512px, so I cut the whole thing for each export.

Each project involves small variations / retouching of this image.

Working on the set is very convenient in my case.

But exporting 10 images manually each time is unnecessarily painful.

Your scripts seem rather suitable yes.
I would be interested in seeing this, at least to understand the structure of the code.
If you want to share I take it with joy  Big Grin

The ofn-tiles script can be found there (it's written in Python, but your Windows Gimp has built-in Python support). See bottom of linked page for installation instructions. See HTML doc inside the ZIP.

You can use it by either specifying the tile size (512x512) or the row and columns (3 rows and 4 columns).
Reply
#7
Ok thank you very much, with all that, it will be very good.  Cool Big Grin
Reply


Forum Jump: