Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Troubles with guide position
#1
Hello everyone!

I'm using gimp to make figures for my scientific papers. Often I need to place a guide in a certain position relative to a selected layer. I'm trying to write a pretty simple script, that do the job. However, I have a problem with it. I want to have a checkbox, with indicates whether the guide should be placed within the selected layer or not. But it doesn't work. Every time, the script return negative value for guide position. If I have a multilayer image, this means that the guide is placed outside the active layer, regardless of the state of the checkbox. If I have a single layer image, I get an error, that the guide is outside the image. Could anyone help me to solve this problem? The full script is below. Thank you!

Code:
(define (script-fu-custom-guide image
                        drawable
                        orientation
                        position
                        in_toggle)

 (let* (
        (lwidth (car (gimp-drawable-get-width image)))
        (lheight (car (gimp-drawable-get-height image)))

        (offx (car (gimp-drawable-get-offsets drawable)))
        (offy (cadr (gimp-drawable-get-offsets drawable)))

        (if (= in_toggle TRUE) (position (* -1 position)))

        (guide_position
          (cond ((= orientation 0) (- offx position)) ; guide on left
                ((= orientation 1) (+ offx lwidth position)) ; guide on right
                ((= orientation 2) (- offy position)) ; guide on top
                ((= orientation 3) (+ offy lheight position)) ; guide on bottom
                )
          )
        )

   (if (< 1 orientation)
     (gimp-image-add-hguide image guide_position)
     )
   (if (> 2 orientation)
     (gimp-image-add-vguide image guide_position)
     )
   (gimp-displays-flush)
   )
 )

(script-fu-register "script-fu-custom-guide"
 _"_Custom guide"
 _"Adds a vertical or horizontal guide to the image"
 ""
 ""
 ""
 "*"
 SF-IMAGE       "Input image"    0
 SF-DRAWABLE    "Input drawable" 0
 SF-OPTION _"_Orientation"         '(_"Left" _"Right" _"Top" _"Bottom")
 SF-ADJUSTMENT _"_Position"         (list 20 0 MAX-IMAGE-SIZE 1 10 0 1)
 SF-TOGGLE     _"Within a layer?"  FALSE
)

(script-fu-menu-register "script-fu-custom-guide"
                        "<Image>/Figures")
Reply
#2
(11-05-2022, 12:19 AM)alvk Wrote: Hello everyone!

I'm using gimp to make figures for my scientific papers. Often I need to place a guide in a certain position relative to a selected layer. I'm trying to write a pretty simple script, that do the job. However, I have a problem with it. I want to have a checkbox, with indicates whether the guide should be placed within the selected layer or not. But it doesn't work. Every time, the script return negative value for guide position. If I have a multilayer image, this means that the guide is placed outside the active layer, regardless of the state of the checkbox. If I have a single layer image, I get an error, that the guide is outside the image. Could anyone help me to solve this problem? The full script is below. Thank you!

Code:
(define (script-fu-custom-guide image
                        drawable
                        orientation
                        position
                        in_toggle)

 (let* (
        (lwidth (car (gimp-drawable-get-width image)))
        (lheight (car (gimp-drawable-get-height image)))

        (offx (car (gimp-drawable-get-offsets drawable)))
        (offy (cadr (gimp-drawable-get-offsets drawable)))

        (if (= in_toggle TRUE) (position (* -1 position)))

        (guide_position
          (cond ((= orientation 0) (- offx position)) ; guide on left
                ((= orientation 1) (+ offx lwidth position)) ; guide on right
                ((= orientation 2) (- offy position)) ; guide on top
                ((= orientation 3) (+ offy lheight position)) ; guide on bottom
                )
          )
        )

   (if (< 1 orientation)
     (gimp-image-add-hguide image guide_position)
     )
   (if (> 2 orientation)
     (gimp-image-add-vguide image guide_position)
     )
   (gimp-displays-flush)
   )
 )

(script-fu-register "script-fu-custom-guide"
 _"_Custom guide"
 _"Adds a vertical or horizontal guide to the image"
 ""
 ""
 ""
 "*"
 SF-IMAGE       "Input image"    0
 SF-DRAWABLE    "Input drawable" 0
 SF-OPTION _"_Orientation"         '(_"Left" _"Right" _"Top" _"Bottom")
 SF-ADJUSTMENT _"_Position"         (list 20 0 MAX-IMAGE-SIZE 1 10 0 1)
 SF-TOGGLE     _"Within a layer?"  FALSE
)

(script-fu-menu-register "script-fu-custom-guide"
                        "<Image>/Figures")

Hello alvk,

There's quite a lot wrong so here's a new version which I think is doing what you wanted:

Code:
(define (script-fu-custom-guide image drawable orientation position in-toggle)

  (let ((image-width (car (gimp-image-width image)))
        (image-height (car (gimp-image-height image)))
        (layer-width (car (gimp-drawable-width drawable)))
        (layer-height (car (gimp-drawable-height drawable)))
        (offx (car (gimp-drawable-offsets drawable)))
        (offy (cadr (gimp-drawable-offsets drawable)))
        (guide-position 0))

    (if (= in-toggle TRUE)
      (set! position (- position)))

    (set! guide-position
      (case orientation
        ((0) (- offx position))                ; guide on left
        ((1) (+ offx layer-width position))    ; guide on right
        ((2) (- offy position))                ; guide on top
        ((3) (+ offy layer-height position)))) ; guide on bottom

    (case orientation
      ((0 1) (if (<= 0 guide-position image-width)
                 (gimp-image-add-vguide image guide-position)))
      ((2 3) (if (<= 0 guide-position image-height)
                 (gimp-image-add-hguide image guide-position))))

    (gimp-displays-flush)))

(script-fu-register "script-fu-custom-guide"
 _"_Custom guide"
 _"Adds a vertical or horizontal guide to the image"
 ""
 ""
 ""
 "*"
 SF-IMAGE       "Input image"     0
 SF-DRAWABLE    "Input drawable"  0
 SF-OPTION     _"_Orientation"   '("_Left" "_Right" "_Top" "_Bottom")
 SF-ADJUSTMENT _"_Position"       (list 20 0 MAX-IMAGE-SIZE 1 10 0 1)
 SF-TOGGLE     _"Within a layer?" FALSE
)

(script-fu-menu-register "script-fu-custom-guide"
                         "<Image>/Figures")
Reply
#3
“Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?”  ― Brian Kernighan

Not a scheme expert. Code typically too clever to be debuggable (I don't even know how to print a value in Scheme...).  You obviously try to play a clever trick to get the guide position but it it obviously wrong: if you are layer or image-centric, this doesn't just change the sign of the position.

IMHO, the simplest way:
  • If you work on an image, then offsets are 0,0, and W&H are the image dimension
  • If you work on layer, then offset are the layer's offset and W&H are the layers dimension
Then your guides are at
  • offsetX+position (left),
  • offsetY+position (top),
  • offsetX+width-position (right),
  • offsetY+height-position (bottom).
Also, despite not being a Scheme person:
  • I had to drop the get bit in  your gimp-drawable-get-{offset,width,height} (ie, gimp-drawable-height is what works for me, what version of Gimp are you using)?
  •  (if (= in_toggle TRUE) ...) is better written (if in_toggle ...)
  •  (* -1 position) is better written (- position)
If you have Python support in your Gimp and always put the guides at the same place relative to image or layers, you can try my ofn-guide-presets script (use it as is or steal the code).
Reply
#4
(11-05-2022, 08:49 AM)teapot Wrote:
(11-05-2022, 12:19 AM)alvk Wrote: Hello everyone!

I'm using gimp to make figures for my scientific papers. Often I need to place a guide in a certain position relative to a selected layer. I'm trying to write a pretty simple script, that do the job. However, I have a problem with it. I want to have a checkbox, with indicates whether the guide should be placed within the selected layer or not. But it doesn't work. Every time, the script return negative value for guide position. If I have a multilayer image, this means that the guide is placed outside the active layer, regardless of the state of the checkbox. If I have a single layer image, I get an error, that the guide is outside the image. Could anyone help me to solve this problem? The full script is below. Thank you!

Code:
(define (script-fu-custom-guide image
                        drawable
                        orientation
                        position
                        in_toggle)

 (let* (
        (lwidth (car (gimp-drawable-get-width image)))
        (lheight (car (gimp-drawable-get-height image)))

        (offx (car (gimp-drawable-get-offsets drawable)))
        (offy (cadr (gimp-drawable-get-offsets drawable)))

        (if (= in_toggle TRUE) (position (* -1 position)))

        (guide_position
          (cond ((= orientation 0) (- offx position)) ; guide on left
                ((= orientation 1) (+ offx lwidth position)) ; guide on right
                ((= orientation 2) (- offy position)) ; guide on top
                ((= orientation 3) (+ offy lheight position)) ; guide on bottom
                )
          )
        )

   (if (< 1 orientation)
     (gimp-image-add-hguide image guide_position)
     )
   (if (> 2 orientation)
     (gimp-image-add-vguide image guide_position)
     )
   (gimp-displays-flush)
   )
 )

(script-fu-register "script-fu-custom-guide"
 _"_Custom guide"
 _"Adds a vertical or horizontal guide to the image"
 ""
 ""
 ""
 "*"
 SF-IMAGE       "Input image"    0
 SF-DRAWABLE    "Input drawable" 0
 SF-OPTION _"_Orientation"         '(_"Left" _"Right" _"Top" _"Bottom")
 SF-ADJUSTMENT _"_Position"         (list 20 0 MAX-IMAGE-SIZE 1 10 0 1)
 SF-TOGGLE     _"Within a layer?"  FALSE
)

(script-fu-menu-register "script-fu-custom-guide"
                        "<Image>/Figures")

Hello alvk,

There's quite a lot wrong so here's a new version which I think is doing what you wanted:

Code:
(define (script-fu-custom-guide image drawable orientation position in-toggle)

  (let ((image-width (car (gimp-image-width image)))
        (image-height (car (gimp-image-height image)))
        (layer-width (car (gimp-drawable-width drawable)))
        (layer-height (car (gimp-drawable-height drawable)))
        (offx (car (gimp-drawable-offsets drawable)))
        (offy (cadr (gimp-drawable-offsets drawable)))
        (guide-position 0))

    (if (= in-toggle TRUE)
      (set! position (- position)))

    (set! guide-position
      (case orientation
        ((0) (- offx position))                ; guide on left
        ((1) (+ offx layer-width position))    ; guide on right
        ((2) (- offy position))                ; guide on top
        ((3) (+ offy layer-height position)))) ; guide on bottom

    (case orientation
      ((0 1) (if (<= 0 guide-position image-width)
                 (gimp-image-add-vguide image guide-position)))
      ((2 3) (if (<= 0 guide-position image-height)
                 (gimp-image-add-hguide image guide-position))))

    (gimp-displays-flush)))

(script-fu-register "script-fu-custom-guide"
 _"_Custom guide"
 _"Adds a vertical or horizontal guide to the image"
 ""
 ""
 ""
 "*"
 SF-IMAGE       "Input image"     0
 SF-DRAWABLE    "Input drawable"  0
 SF-OPTION     _"_Orientation"   '("_Left" "_Right" "_Top" "_Bottom")
 SF-ADJUSTMENT _"_Position"       (list 20 0 MAX-IMAGE-SIZE 1 10 0 1)
 SF-TOGGLE     _"Within a layer?" FALSE
)

(script-fu-menu-register "script-fu-custom-guide"
                         "<Image>/Figures")

Thank you! Everything works just fine!

(11-05-2022, 09:20 AM)Ofnuts Wrote: “Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?”  ― Brian Kernighan

Not a scheme expert. Code typically too clever to be debuggable (I don't even know how to print a value in Scheme...).  You obviously try to play a clever trick to get the guide position but it it obviously wrong: if you are layer or image-centric, this doesn't just change the sign of the position.

IMHO, the simplest way:
  • If you work on an image, then offsets are 0,0, and W&H are the image dimension
  • If you work on layer, then offset are the layer's offset and W&H are the layers dimension
Then your guides are at
  • offsetX+position (left),
  • offsetY+position (top),
  • offsetX+width-position (right),
  • offsetY+height-position (bottom).
Also, despite not being a Scheme person:
  • I had to drop the get bit in  your gimp-drawable-get-{offset,width,height} (ie, gimp-drawable-height is what works for me, what version of Gimp are you using)?
  •  (if (= in_toggle TRUE) ...) is better written (if in_toggle ...)
  •  (* -1 position) is better written (- position)
If you have Python support in your Gimp and always put the guides at the same place relative to image or layers, you can try my ofn-guide-presets script (use it as is or steal the code).

Thank you for your reply. I'm using the development version of Gimp, installed from Debian repository. Unfortunately, I have no python support in my version of Gimp. As far as I know, there is no support for python2 in Debian. Strangely, I didn't find python3 support in the development version of Gimp from the repository. Probably, they compiled it without python support?

(11-05-2022, 09:20 AM)Ofnuts Wrote: I had to drop the get bit in  your gimp-drawable-get-{offset,width,height} (ie, gimp-drawable-height is what works for me, what version of Gimp are you using)?
In Gimp3 on my computer, gimp-drawable-height and others doesn't work without get. Probably they have changed something?
Reply
#5
(11-05-2022, 10:00 AM)alvk Wrote:
(11-05-2022, 08:49 AM)teapot Wrote:
(11-05-2022, 12:19 AM)alvk Wrote: Hello everyone!

I'm using gimp to make figures for my scientific papers. Often I need to place a guide in a certain position relative to a selected layer. I'm trying to write a pretty simple script, that do the job. However, I have a problem with it. I want to have a checkbox, with indicates whether the guide should be placed within the selected layer or not. But it doesn't work. Every time, the script return negative value for guide position. If I have a multilayer image, this means that the guide is placed outside the active layer, regardless of the state of the checkbox. If I have a single layer image, I get an error, that the guide is outside the image. Could anyone help me to solve this problem? The full script is below. Thank you!

Code:
(define (script-fu-custom-guide image
                        drawable
                        orientation
                        position
                        in_toggle)

 (let* (
        (lwidth (car (gimp-drawable-get-width image)))
        (lheight (car (gimp-drawable-get-height image)))

        (offx (car (gimp-drawable-get-offsets drawable)))
        (offy (cadr (gimp-drawable-get-offsets drawable)))

        (if (= in_toggle TRUE) (position (* -1 position)))

        (guide_position
          (cond ((= orientation 0) (- offx position)) ; guide on left
                ((= orientation 1) (+ offx lwidth position)) ; guide on right
                ((= orientation 2) (- offy position)) ; guide on top
                ((= orientation 3) (+ offy lheight position)) ; guide on bottom
                )
          )
        )

   (if (< 1 orientation)
     (gimp-image-add-hguide image guide_position)
     )
   (if (> 2 orientation)
     (gimp-image-add-vguide image guide_position)
     )
   (gimp-displays-flush)
   )
 )

(script-fu-register "script-fu-custom-guide"
 _"_Custom guide"
 _"Adds a vertical or horizontal guide to the image"
 ""
 ""
 ""
 "*"
 SF-IMAGE       "Input image"    0
 SF-DRAWABLE    "Input drawable" 0
 SF-OPTION _"_Orientation"         '(_"Left" _"Right" _"Top" _"Bottom")
 SF-ADJUSTMENT _"_Position"         (list 20 0 MAX-IMAGE-SIZE 1 10 0 1)
 SF-TOGGLE     _"Within a layer?"  FALSE
)

(script-fu-menu-register "script-fu-custom-guide"
                        "<Image>/Figures")

Hello alvk,

There's quite a lot wrong so here's a new version which I think is doing what you wanted:

Code:
(define (script-fu-custom-guide image drawable orientation position in-toggle)

  (let ((image-width (car (gimp-image-width image)))
        (image-height (car (gimp-image-height image)))
        (layer-width (car (gimp-drawable-width drawable)))
        (layer-height (car (gimp-drawable-height drawable)))
        (offx (car (gimp-drawable-offsets drawable)))
        (offy (cadr (gimp-drawable-offsets drawable)))
        (guide-position 0))

    (if (= in-toggle TRUE)
      (set! position (- position)))

    (set! guide-position
      (case orientation
        ((0) (- offx position))                ; guide on left
        ((1) (+ offx layer-width position))    ; guide on right
        ((2) (- offy position))                ; guide on top
        ((3) (+ offy layer-height position)))) ; guide on bottom

    (case orientation
      ((0 1) (if (<= 0 guide-position image-width)
                 (gimp-image-add-vguide image guide-position)))
      ((2 3) (if (<= 0 guide-position image-height)
                 (gimp-image-add-hguide image guide-position))))

    (gimp-displays-flush)))

(script-fu-register "script-fu-custom-guide"
 _"_Custom guide"
 _"Adds a vertical or horizontal guide to the image"
 ""
 ""
 ""
 "*"
 SF-IMAGE       "Input image"     0
 SF-DRAWABLE    "Input drawable"  0
 SF-OPTION     _"_Orientation"   '("_Left" "_Right" "_Top" "_Bottom")
 SF-ADJUSTMENT _"_Position"       (list 20 0 MAX-IMAGE-SIZE 1 10 0 1)
 SF-TOGGLE     _"Within a layer?" FALSE
)

(script-fu-menu-register "script-fu-custom-guide"
                         "<Image>/Figures")

Thank you! Everything works just fine!

(11-05-2022, 09:20 AM)Ofnuts Wrote: “Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?”  ― Brian Kernighan

Not a scheme expert. Code typically too clever to be debuggable (I don't even know how to print a value in Scheme...).  You obviously try to play a clever trick to get the guide position but it it obviously wrong: if you are layer or image-centric, this doesn't just change the sign of the position.

IMHO, the simplest way:
  • If you work on an image, then offsets are 0,0, and W&H are the image dimension
  • If you work on layer, then offset are the layer's offset and W&H are the layers dimension
Then your guides are at
  • offsetX+position (left),
  • offsetY+position (top),
  • offsetX+width-position (right),
  • offsetY+height-position (bottom).
Also, despite not being a Scheme person:
  • I had to drop the get bit in  your gimp-drawable-get-{offset,width,height} (ie, gimp-drawable-height is what works for me, what version of Gimp are you using)?
  •  (if (= in_toggle TRUE) ...) is better written (if in_toggle ...)
  •  (* -1 position) is better written (- position)
If you have Python support in your Gimp and always put the guides at the same place relative to image or layers, you can try my ofn-guide-presets script (use it as is or steal the code).

Thank you for your reply. I'm using the development version of Gimp, installed from Debian repository. Unfortunately, I have no python support in my version of Gimp. As far as I know, there is no support for python2 in Debian. Strangely, I didn't find python3 support in the development version of Gimp from the repository. Probably, they compiled it without python support?

(11-05-2022, 09:20 AM)Ofnuts Wrote: I had to drop the get bit in  your gimp-drawable-get-{offset,width,height} (ie, gimp-drawable-height is what works for me, what version of Gimp are you using)?
In Gimp3 on my computer, gimp-drawable-height and others doesn't work without get. Probably they have changed something?

If you are using Gimp3, it should have python support, because it uses Python V3, but this support is somewhat different... Gimp v3 is still in a state of flux, some things can change between releases (for instance, the latest release introduced multiple layer selection...), so it's not fit for "production" IMHO.
Reply
#6
(11-05-2022, 10:41 AM)Ofnuts Wrote: If you are using Gimp3, it should have python support, because it uses Python V3, but this support is somewhat different... Gimp v3 is still in a state of flux, some things can change between releases (for instance, the latest release introduced multiple layer selection...), so it's not fit for "production" IMHO.

I know that Gimp3 has python support, but I didn't find any python related stuff in Filters menu when I'd installed it. I don't know why. Yes, I agree with you that Gimp3 is not ready yet. I was curious to see how it looks like when I was installing it. In my work, I need to align several pictures on a single page, and even an unstable version of Gimp does the job.
Reply


Forum Jump: