Post Reply 

Create new images button & Save as override from Script-fu

Sep 5, 2014, 12:42
Post: #1
Create new images button & Save as override from Script-fu
Hi everyone,

I am new to Script-fu and this is my first post on this forum.

Does somebody can help me creating a button to create a new image with specific size & another button to save this image as jpeg to a specific location with a specific name and override if already exist ?

I am using Gimp 2.8 for Windows.

I made several attempt but I cannot managed to get anything work Sad

Thank you
Find all posts by this user
Quote this message in a reply
Sep 5, 2014, 13:12
Post: #2
RE: Create new images button & Save as override from Script-fu
If you show us what you have done so far we can talk you through your problems.

Kevin
Find all posts by this user
Quote this message in a reply
Sep 8, 2014, 11:36
Post: #3
RE: Create new images button & Save as override from Script-fu
Hello Kevin,
thank you for your reply.

I use this script to create my image :

(define (gimp-image-new 200 100 1)
)
(script-fu-register "script-fu-File-Image" ;; name of the script
"<Image>/File/New/Image" ;; position in the menu
"Create a layer, no dialog" ;;
"Autor" ;; autor
"Copyright" ;; copyright
"Date" ;; date
"" ;; type of images supported by the script
SF-IMAGE "Image" 0 ;; image where layer is added
SF-DRAWABLE "Drawable" 0 ;; active layer
) ;; end of register

But when I run the script I get this error message:
Error while executing script-fu-File-Image:
Error: (:2) eval: unbound variable: script-fu-File-Image

Thank you,
Benoit

(Sep 5, 2014 13:12)paynekj Wrote:  If you show us what you have done so far we can talk you through your problems.

Kevin
Find all posts by this user
Quote this message in a reply
Sep 8, 2014, 13:13
Post: #4
RE: Create new images button & Save as override from Script-fu
(Sep 8, 2014 11:36)beniman Wrote:  Hello Kevin,
thank you for your reply.

I use this script to create my image :


But when I run the script I get this error message:
Error while executing script-fu-File-Image:
Error: (:2) eval: unbound variable: script-fu-File-Image

Thank you,
Benoit

You have a few things wrong. Here's a version that works for you to continue with:
Code:
(define (script-fu-File-Image)
  (let* (
          (image 0)
        )
     (set! image (car (gimp-image-new 200 100 1)))
     (gimp-display-new image)
     (gimp-displays-flush)
  )
)

(script-fu-register "script-fu-File-Image" ;; name of the script
  "<Image>/File/Benoit" ;; position in the menu
  "Create a layer, no dialog" ;;
  "Autor" ;; autor
  "Copyright" ;; copyright
  "Date" ;; date
  "" ;; type of images supported by the script
) ;; end of register

Kevin
Find all posts by this user
Quote this message in a reply
Sep 9, 2014, 08:30
Post: #5
RE: Create new images button & Save as override from Script-fu
Thank you Kevin.

I can create my image!
Now I want to save it as jpg can you help me, here is my code:


(define (script-fu-File-Image)
(let* (
(image 0)
(layer 0)
)
(set! image (car (gimp-image-new 200 100 RGB)))
(set! layer (car (gimp-layer-new image 200 100 RGB-IMAGE "layer" 100 NORMAL)))
(gimp-image-insert-layer image layer 0 -1)
(gimp-context-set-background '(255 255 255))
(gimp-drawable-fill layer BACKGROUND-FILL)
(gimp-display-new image)
(gimp-displays-flush)
)
)

(define (FU_save-as-jpg)
(let* (
(image 0)
(drawable 0)
)
(gimp-image-set-filename image "name")
(file-jpeg-save 0 image drawable "name" "name" 1 1 1 1 "GIMP" 0 0 0 0 )
)
)

(script-fu-register "script-fu-File-Image" ;; name of the script
"<Image>/File/New/Benoit" ;; position in the menu
"Create a layer, no dialog" ;;
"Autor" ;; autor
"Copyright" ;; copyright
"Date" ;; date
"" ;; type of images supported by the script
) ;; end of register

(script-fu-register "FU_save-as-jpg" ;; name of the script
"Save as JPG"
"<Image>/File/Save as JPEG" ;; position in the menu
"Create a layer, no dialog" ;;
"Autor" ;; autor
"Copyright" ;; copyright
"Date" ;; date
"" ;; type of images supported by the script
) ;; end of register

(script-fu-menu-register "FU_save-as-jpg"
"<Image>/File/Save/"
)
Find all posts by this user
Quote this message in a reply
Sep 9, 2014, 10:15
Post: #6
RE: Create new images button & Save as override from Script-fu
Your
Code:
(define (FU_save-as-jpg)
needs to be passed the image and drawable IDs and the registration needs to include SF-IMAGE and SF_DRAWABLE parameters:
Code:
(define (script-fu-File-Image)
  (let* (
           (image 0)
           (layer 0)
         )
    (set! image (car (gimp-image-new 200 100 RGB)))
    (set! layer (car (gimp-layer-new image 200 100 RGB-IMAGE "layer" 100 NORMAL)))
    (gimp-image-insert-layer image layer 0 -1)
    (gimp-context-set-background '(255 255 255))
    (gimp-drawable-fill layer BACKGROUND-FILL)
    (gimp-display-new image)
    (gimp-displays-flush)
  )
)

(define (FU_save-as-jpg image drawable)
(let* (
         (filename "name.jpg")
       )
    (gimp-image-set-filename image filename)
    (file-jpeg-save RUN-NONINTERACTIVE image drawable filename filename 1 1 1 1 "GIMP" 0 0 0 0 )
  )
)

(script-fu-register "script-fu-File-Image" ;; name of the script
  "<Image>/File/New/Benoit" ;; position in the menu
  "Create a layer, no dialog" ;;
  "Autor" ;; autor
  "Copyright" ;; copyright
  "Date" ;; date
  "" ;; type of images supported by the script
) ;; end of register

(script-fu-register "FU_save-as-jpg" ;; name of the script
                    "Save as JPEG"   ;;; menu label
                    "Export image as a JPEG" ;; tipstrip
                    "Copyright"     ;; copyright
                    "selfauthor"    ;; Author
                    "selffileversion" ;; date
                    "*"                ;; image types
                    SF-IMAGE "Image" 0          ;; image
                    SF-DRAWABLE "Drawable" 0    ;; drawable
)

(script-fu-menu-register "FU_save-as-jpg" "<Image>/File/Save" )

Kevin
Find all posts by this user
Quote this message in a reply
Sep 9, 2014, 13:03 (This post was last modified: Sep 9, 2014 13:05 by beniman.)
Post: #7
RE: Create new images button & Save as override from Script-fu
Thank you for all Kevin.

One last point:
this command has no effect:
(gimp-context-set-paint-method "gimp-paintbrush")

Do you have any clue ?

Here is my full code:

(define (script-fu-File-Image)
(let* (
(image 0)
(layer 0)
(method "gimp-paintbrush")
(brush "1. Pixel")
)
(set! image (car (gimp-image-new 200 100 RGB)))
(set! layer (car (gimp-layer-new image 200 100 RGB-IMAGE "layer" 100 NORMAL)))
(gimp-image-insert-layer image layer 0 -1)
(gimp-context-set-background '(255 255 255))
(gimp-drawable-fill layer BACKGROUND-FILL)
(gimp-context-set-paint-method "gimp-paintbrush")
(gimp-context-set-paint-mode 0)
(gimp-context-set-brush brush)
(gimp-context-set-brush-size 3)
(gimp-display-new image)
(gimp-displays-flush)
)
)

(define (FU_save-as-jpg image drawable)
(let* (
(filename "/Users/bde/Desktop/name.jpg")
(display 1)
)
(gimp-image-set-filename image filename)
(file-jpeg-save RUN-NONINTERACTIVE image drawable filename filename 1 1 1 1 "GIMP" 0 0 0 0 )
(gimp-display-delete display)
)

)

(script-fu-register "script-fu-File-Image" ;; name of the script
"<Image>/File/New/Benoit" ;; position in the menu
"Create a layer, no dialog" ;;
"Autor" ;; autor
"Copyright" ;; copyright
"Date" ;; date
"" ;; type of images supported by the script
) ;; end of register

(script-fu-register "FU_save-as-jpg" ;; name of the script
"Save as JPEG" ;;; menu label
"Export image as a JPEG" ;; tipstrip
"Copyright" ;; copyright
"selfauthor" ;; Author
"selffileversion" ;; date
"*" ;; image types
SF-IMAGE "Image" 0 ;; image
SF-DRAWABLE "Drawable" 0 ;; drawable
)

(script-fu-menu-register "FU_save-as-jpg" "<Image>/File/Save" )
Regards,
Benoit
Find all posts by this user
Quote this message in a reply
Sep 9, 2014, 13:35
Post: #8
RE: Create new images button & Save as override from Script-fu
(Sep 9, 2014 13:03)beniman Wrote:  Thank you for all Kevin.

One last point:
this command has no effect:
(gimp-context-set-paint-method "gimp-paintbrush")

Do you have any clue ?

How do you know it doesn't do anything? I ask because that function sets the paint method, but you haven't done any painting in your script.

If you are expecting it to change the tool selected to the paintbrush in the toolbox then you will be disappointed. Scripts are not allowed to alter the user interface.

Kevin
Find all posts by this user
Quote this message in a reply
Sep 9, 2014, 13:50
Post: #9
RE: Create new images button & Save as override from Script-fu
(Sep 9, 2014 13:35)paynekj Wrote:  If you are expecting it to change the tool selected to the paintbrush in the toolbox then you will be disappointed. Scripts are not allowed to alter the user interface.

Kevin

Exactly, I want to set the paintbrush as the selected tool in the toolbox.

Those lines
(gimp-context-set-paint-mode 0)
(gimp-context-set-brush brush)
(gimp-context-set-brush-size 3)
are well executed and set the interface as expected.

As you said, "Scripts are not allowed to alter the user interface." Do you have any workaround to allow a tool to be selected when the file is created ?

Another point, do you know how I can set the location in the menu where the script will be inserted. I would like it at the top of the File menu.
Find all posts by this user
Quote this message in a reply
Sep 9, 2014, 14:36
Post: #10
RE: Create new images button & Save as override from Script-fu
Sadly my answer to both questions is No. - there is no workaround and I don't think you can place a script menu item at the top of the File menu.

Kevin
Find all posts by this user
Quote this message in a reply
Sep 9, 2014, 14:40
Post: #11
RE: Create new images button & Save as override from Script-fu
Well ok thank you Kevin.
You have been very helpful.

We can close this thread.

Best regards,
Benoit
Find all posts by this user
Quote this message in a reply
Sep 10, 2014, 10:10
Post: #12
RE: Create new images button & Save as override from Script-fu
(Sep 9, 2014 14:52)ofnuts Wrote:  You can put the open menu in File>Create.

But if you use this often, you don't care where the menus entries show up because you can associate the create/save actions to a keyboard shortcut instead.

Nice hint !

My biggest concern is the ability to set the paintbrush as default tool in my script.
It seems possible as per this forum:
http://www.gimpchat.com/viewtopic.php?f=9&t=2113 reply of saulgoode
I know it is not the official forum but...

Sorry Kevin, but I am still asking this in case someone have the key.
Find all posts by this user
Quote this message in a reply
Sep 10, 2014, 10:57 (This post was last modified: Sep 10, 2014 10:59 by paynekj.)
Post: #13
RE: Create new images button & Save as override from Script-fu
(Sep 10, 2014 10:10)beniman Wrote:  I know it is not the official forum but...

There isn't an official forum, the closest you can get is either of the mailing lists, or IRC: http://www.gimp.org/mail_lists.html

It's also important to note that GIMP developers aren't observably contributing to any of the unofficial forums.

Saul is just repeating what you have already done in your script, which is to control the paint_method to be used by the script.

And there's no need to be sorry about persevering with your quest Wink

Kevin
Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Removing color from multiple images Aquilo 6 2,914 Sep 28, 2017 12:56
Last Post: angel0sdh
  nothing works from the File menu, can't save, export, close, etc jperiod 1 137 Aug 3, 2017 05:19
Last Post: ythgilb
  How to overlap two images? ferguson1951 1 161 Jul 4, 2017 07:10
Last Post: ythgilb
  Default save-as location Babudro 1 198 Jun 7, 2017 15:44
Last Post: ythgilb
  Putting two images side by side chrisj 3 6,940 May 30, 2017 11:45
Last Post: blighty

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS