It is currently Sat Jun 29, 2024 12:08 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Struggling to write a basic script
PostPosted: Mon Feb 05, 2024 4:05 pm  (#1) 
Offline
New Member

Joined: Feb 05, 2024
Posts: 3
Hello,

I'm really struggling to write a basic non-interactive script. I have read many tutorials and can't make any sense of the errors I'm getting.

The function of the script is to automate a series of events that I perform repetitively on images where a portrait has redeye, namely to feather a selection (which I make manually) and change the hue-saturation values all within the currently active, single layer image.

I even tried getting ChatGPT to write a script!

When I refresh scripts I get a generic 'error while loading' message.

I would be very grateful for any help in figuring out what I'm doing wrong. I feel like it's something very basic that I can't understand about the script layout.

(define (redeye)
(let* (
(image (car (gimp-image-list)))
(drawable (car (gimp-image-get-active-drawable image)))
(selection (car (gimp-selection-save image)))
)

; Check if there is an active selection
(if (= selection 0)
(begin
(gimp-message "No active selection. Please make a selection and try again.")
(return))
; Feather the selection
(gimp-selection-feather image 4)
)

; Change Hue and Saturation
(gimp-drawable-hue-saturation drawable 30 0.5 0.7 0.2)

; Update the display
(gimp-displays-flush)
)

; Register the script
(script-fu-register "Redeye"
"Redeye"
"Feathers the current selection and changes hue/saturation"
"Peter Freeth"
"Peter Freeth"
"2024"
"<Image>/Filters/Redeye..."
""
"RGB*"
SF-ADJUSTMENT "Feather Radius" '(10 1 100 1 10 0 0)
)

; Run the script
(redeye)


Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: Struggling to write a basic script
PostPosted: Mon Feb 05, 2024 6:11 pm  (#2) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4751
I don't write my scripts in Scheme but:

1) "(image (car (gimp-image-list)))": cardinal sin; this makes you script runnable only if there is a single image opened in Gimp. Normaly the registration should start with a SF_IMAGE argument, which is pre-filled by Gimp with the active image when the script is called.

2) "(drawable (car (gimp-image-get-active-drawable image)))": same as above, if you second argument is a SF_DRAWABLE, it is automatically pre-filled with the active layer (a "drawable" is either a layer, a layer mask or a channel)

3) "(define (redeye)": your function definition should declare at least the feather radius argument, and if you follow the two remarks above, the image and drawable/layer arguments

4) "(selection (car (gimp-selection-save image)))": why are you doing this? You don't need a "selection variable" since the "selection-feather" will apply anyway to the current selection.

5) To test if you have a selection use "(gimp-selection-is-empty image)"

6) Can't tell the purpose of the begin/return (looks like suddenly you switched to Basic). In scheme I expect something like "(if (condition)(list of thing to do if true)(list of things to do if false))" (but don't take my word for it)

7) "(gimp-displays-flush)": rarely necessary

8) "(script-fu-register "Redeye"": the first argument is the script identifier, I think it should be all lowercase.

9) I don't understand that empty string just before the "RGB*", possibly unwanted.

10) "SF-ADJUSTMENT "Feather Radius" '(10 1 100 1 10 0 0)": the arguments should be in a list (even if there is only one, but of you follow my recommendations you will have three)

11) SF-ADJUSTMENT syntax is likely wrong. Possibly (if I compare to Python): "(SF-ADJUSTMENT "radius" "Feather Radius" 10 ( 1 100 1)", syntax being, SF-ADJUSTMENT, variable name, variable description, default value, and description of the spinner in a list (low value, high value, step).

12) There should be a list of returned items just after the list of arguments (very often just an empty list)

13) The registration should include the name of the function, so

14) the "(redeye)" call at the bottom may need to be in the registration list (without parentheses)

_________________
Image


Top
 Post subject: Re: Struggling to write a basic script
PostPosted: Mon Feb 05, 2024 10:14 pm  (#3) 
Offline
GimpChat Member
User avatar

Joined: Dec 09, 2018
Posts: 656
I don't write much scheme either but hopefully this will help get you started.


(define (script-fu-red-eye image drawable feather-radius)

  (if (= (car (gimp-selection-is-empty image)) TRUE)
    (gimp-message "No active selection. Please make a selection and try again.")
    (begin
      (gimp-image-undo-group-start image)
      (gimp-selection-feather image feather-radius)

      ; What hue-range do you want?
      ;   HUE-RANGE-ALL (0)
      ;   HUE-RANGE-RED (1)
      ;   HUE-RANGE-YELLOW (2)
      ;   HUE-RANGE-GREEN (3)
      ;   HUE-RANGE-CYAN (4)
      ;   HUE-RANGE-BLUE (5)
      ;   HUE-RANGE-MAGENTA (6)
      (gimp-drawable-hue-saturation drawable HUE-RANGE-ALL 30 0.5 0.7 0.2)

      (gimp-image-undo-group-end image)
      (gimp-displays-flush)))
)

(script-fu-register
  "script-fu-red-eye"                         ; Name
  "Red Eye"                                   ; Menu label
  "Feathers the current selection and changes hue/saturation" ; Description
  "Peter Freeth"                              ; Author
  "Peter Freeth"                              ; Copyright
  "Feb 2024"                                  ; Date
  "RGB*"                                      ; Types
  SF-IMAGE       "Input image"     0
  SF-DRAWABLE    "Input drawable"  0
  SF-ADJUSTMENT  "Feather Radius"        (list 10 1 100 1 10 0 SF-SPINNER)
; SF-ADJUSTMENT  "Feather Radius"       '(10 1 100 1 10 0 0)
)

; SF-ADJUSTMENT "Label" '(initial-value lower-limit upper-limit step-increment page-increment digits type)
; step-increment  Increment when using the up and down arrows.
; page-increment  Increment when using page up and page down. Keys Fn and Pg Up or Pg Dn.
; digits          Digits after the point (decimal part).
; type            One of: SF-SLIDER or 0, SF-SPINNER or 1.
;
; To define a list composed of literals or previously declared variables, use the list function:
;   (list 5 4 3 a b c)
;
; After changing the script remember to do Filters -> Script-Fu -> Refresh Scripts.

(script-fu-menu-register "script-fu-red-eye" "<Image>/Filters/")



Top
 Post subject: Re: Struggling to write a basic script
PostPosted: Tue Feb 06, 2024 5:13 am  (#4) 
Offline
New Member

Joined: Feb 05, 2024
Posts: 3
Wow, that's incredibly helpful, thank you both.

I managed, late last night, to get my original version working, I didn't understand the local nature of the let variable before. I was very excited to see this morning that it worked first time. And that was the only time it worked! After that, I got "Procedure execution of gimp-image-get-active-drawable failed on invalid input arguments: Procedure 'gimp-image-get-active-drawable' has been called with an invalid ID for argument 'image'".

I'm assuming that's because the image variable hadn't reset.

I will have a closer look, thank you again.


Top
 Post subject: Re: Struggling to write a basic script
PostPosted: Tue Feb 06, 2024 5:47 am  (#5) 
Offline
New Member

Joined: Feb 05, 2024
Posts: 3
OK, so I actually wanted it to operate without any user input and a fixed selection. I think the input box was something that ChatGPT came up with. I figured out how to change that and... it works!

Better than that, though, your comments in the script - teapot - helped me to understand the functions, especially hue range, which has improved the script over my original plan as it now works only on the red eye even if I select a bit of iris too.

When I was looking through the reference manual, I couldn't make sense of how to format the values for the functions, you've helped me to decipher that.

With the redeye script on a keyboard shortcut, you've made my work so much faster and easier, thank you.


Top
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Some basic questions

2

No new posts Attachment(s) Gimp Brushes (basic/obsolete)

0

No new posts Attachment(s) Abstract Art by Discovery out of Geometric/Basic Shapes

10

No new posts GIMP/Graphic Software, Basic Terminology

5

No new posts Attachment(s) GEGL filter that makes Basic Pencil Drawings

11



* Login  



Powered by phpBB3 © phpBB Group