Gimp-Forum.net
Script-FU: Batch rotate script - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: Script-FU: Batch rotate script (/Thread-Script-FU-Batch-rotate-script)



Script-FU: Batch rotate script - dnairb - 02-03-2019

I am trying to write a batch rotate script, to rotate a number of images by an angle which is passed to the script.
I have the following, which is successfully registered in Gimp:
Code:
(script-fu-register
  "script-fu-batch_arbitrary_rotate"
  "Batch Rotation"
  ""
  ""
  ""
  ""
  "*"
)

(script-fu-menu-register "script-fu-batch_arbitrary_rotate" "<Image>/Tools/My scripts")

(
define (script-fu-batch_arbitrary_rotate pattern inDegrees)
  (let*
    (
      (filelist (cadr (file-glob pattern 1)))
    )
    (while
      (not (null? filelist))
      (let*
        (
          (filename (car filelist))
          (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
          (radians ( / (* inDegrees 4 (atan 1.0)) 180)) ;converts angle <inDegrees> to radians
          (gimp-item-transform-rotate image radians FALSE 150 150)
          (drawable (car (gimp-image-get-active-layer image)))
          (gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
        )
        (set! filelist (cdr filelist))
      )
    )
  )
)

This is called in terminal by (to rotate each image by 35 degrees and save):
Code:
gimp -i -b '(script-fu-batch_arbitrary_rotate "*.png" 35)' -b '(gimp-quit 0)'

The terminal output states: "batch command executed successfully", but the images are not rotated. The files' properties suggest that no operations were performed.

I have racked my brains over this; can anyone help with the above script?


RE: Script-FU: Batch rotate script - Ofnuts - 02-04-2019

Not a script-fu expert, but it looks like gimp-item-transform-rotate expects a layer/channel/path, not an image. If you want to rotate the image use gimp-image-rotate.


RE: Script-FU: Batch rotate script - dnairb - 02-04-2019

Thank you for your reply. I looked at gimp-image-rotate, but Procedure Browser states that the rotate-type parameter only accepts ROTATE-90 (0), ROTATE-180 (1), ROTATE-270 (2).
I want to be able rotate the images by an arbitrary angle.


RE: Script-FU: Batch rotate script - programmer_ceds - 02-04-2019

(02-04-2019, 05:48 AM)Ofnuts Wrote: Not a script-fu expert, but it looks like gimp-item-transform-rotate expects a layer/channel/path, not an image. If you want to rotate the image use gimp-image-rotate.
Sorry - misread the problem. Missed that the problem was about rotating the whole image.

I guess the answer is to use gimp-item-transform-rotate on each of the layers in the image. Use gimp-image-get-layers to get a list of the layers in the image and iterate through them.


RE: Script-FU: Batch rotate script - dnairb - 02-04-2019

(02-04-2019, 05:35 PM)programmer_ceds Wrote:
(02-04-2019, 05:48 AM)Ofnuts Wrote: Not a script-fu expert, but it looks like gimp-item-transform-rotate expects a layer/channel/path, not an image. If you want to rotate the image use gimp-image-rotate.
Sorry - misread the problem. Missed that the problem was about rotating the whole image.

I guess the answer is to use gimp-item-transform-rotate on each of the layers in the image. Use gimp-image-get-layers to get a list of the layers in the image and iterate through them.

Each image has only 1 layer.


RE: Script-FU: Batch rotate script - Ofnuts - 02-04-2019

Yes, I read the gimp-image-rotate description a bit too fast Smile But yes, you likely have to rotate layers (in any case the save API also deals with layers and not images).