Gimp-Forum.net
Scale image relative size? - 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: Scale image relative size? (/Thread-Scale-image-relative-size)



Scale image relative size? - BaconWizard17 - 08-14-2022

Hi all! I'm trying to work out a script that I can use to reduce the size of an image by half. However, I need this to work with images of different sizes, but I always want them to be scaled to half their size. I know that this code exists:

Code:
(gimp-image-scale image X Y)

But I've only ever used it for absolute sizes. How can I make it be relative? I'm assuming I'll need to get the current size of the image and then multiply those variables by 0.5, then use the variables in this command, but I'm having trouble finding any documentation about actually getting the layer sizes. Any help is appreciated!


RE: Scale image relative size? - Ofnuts - 08-14-2022

See (gimp-image-width image) and (gimp-image-height image) to get the image width and height. Getting the sizes of layers is pointless here because you'll be scaling the image and not the layers.

The best way to find an answer to this kind of questions if to start the Scrip-fu console (Filters ➤ Script-fu ➤ Console) and then hit the Browse... button and try the terms in the search bar at the top left:

[attachment=8498]



RE: Scale image relative size? - rich2005 - 08-14-2022

Well, for a simplistic, single layer,  plugin. The attached is the one I use to save on a bit of bandwidth. (followed by another plugin to export to a jpeg)  It scales down two-thirds and adds a bit of sharpening. Not difficult to change the scaling factor., disable the sharpening.
I suppose the issue is your multi-layer images.

..and it is python, not script-fu
In the tools menu, no parameters.

If you have lots of images to scale, then I suggest the Gimp batch plugin BIMP or non-Gimp ImageMagick in a .bat file.


RE: Scale image relative size? - BaconWizard17 - 08-25-2022

(08-14-2022, 03:15 PM)Ofnuts Wrote: See (gimp-image-width image) and (gimp-image-height image) to get the image width and height. Getting the sizes of layers is pointless here because you'll be scaling the image and not the layers.

The best way to find an answer to this kind of questions if to start the Scrip-fu console (Filters ➤ Script-fu ➤ Console) and then hit the Browse... button and try the terms in the search bar at the top left:

I've been trying to use these commands in the following script:

(gimp-image-scale image (/ (gimp-image-width image) 2) (/ (gimp-image-height image) 2))

But I keep getting an error that "argument 1 must be a number" for the division operations. Do I need to be assigning the value to a variable somehow?

Edit: managed to figure out how to assign variables and got this far, but now Gimp crashes when I use this:

(gimp-image-undo-group-start image)
(gimp-selection-none image)
(let*
(
(newWidth (gimp-image-width image))
(newHeight (gimp-image-height image))
)
(gimp-image-scale image (/ newWidth 2) (/ newHeight 2))
)
(gimp-displays-flush)
(gimp-image-undo-group-end image)



RE: Scale image relative size? - Kevin - 08-25-2022

You need to know that most GIMP functions in script-fu return a list, not a single value, so you need to extract the first value from the list:

Code:
(newWidth (car (gimp-image-width image)))
(newHeight (car (gimp-image-height image)))



RE: Scale image relative size? - BaconWizard17 - 08-26-2022

(08-25-2022, 07:47 AM)Kevin Wrote: You need to know that most GIMP functions in script-fu return a list, not a single value, so you need to extract the first value from the list:

Code:
(newWidth (car (gimp-image-width image)))
(newHeight (car (gimp-image-height image)))

That makes sense. Unfortunately, I'm still getting the same error with this:
Code:
(define (script-fu-scale-half image)
    (gimp-image-undo-group-start image)
    (gimp-selection-none image)
    (let*
        (
            (newWidth (car (gimp-image-width image)))
            (newHeight (car (gimp-image-height image)))
        )
        (gimp-image-scale image (/ newWidth 2) (/ newHeight 2))
    )
    (gimp-displays-flush)
    (gimp-image-undo-group-end image)
)

Edit: I wish I knew why, but it inexplicably started working without me changing anything (as far as I can tell). It works now!


RE: Scale image relative size? - Ofnuts - 08-26-2022

(08-26-2022, 06:00 PM)BaconWizard17 Wrote:
(08-25-2022, 07:47 AM)Kevin Wrote: You need to know that most GIMP functions in script-fu return a list, not a single value, so you need to extract the first value from the list:

Code:
(newWidth (car (gimp-image-width image)))
(newHeight (car (gimp-image-height image)))

That makes sense. Unfortunately, I'm still getting the same error with this:
Code:
(define (script-fu-scale-half image)
    (gimp-image-undo-group-start image)
    (gimp-selection-none image)
    (let*
        (
            (newWidth (car (gimp-image-width image)))
            (newHeight (car (gimp-image-height image)))
        )
        (gimp-image-scale image (/ newWidth 2) (/ newHeight 2))
    )
    (gimp-displays-flush)
    (gimp-image-undo-group-end image)
)

Edit: I wish I knew why, but it inexplicably started working without me changing anything (as far as I can tell). It works now!

Did you refresh the script (or restart Gimp)?


RE: Scale image relative size? - BaconWizard17 - 08-27-2022

(08-26-2022, 07:14 PM)Ofnuts Wrote:
(08-26-2022, 06:00 PM)BaconWizard17 Wrote:
(08-25-2022, 07:47 AM)Kevin Wrote: You need to know that most GIMP functions in script-fu return a list, not a single value, so you need to extract the first value from the list:

Code:
(newWidth (car (gimp-image-width image)))
(newHeight (car (gimp-image-height image)))

That makes sense. Unfortunately, I'm still getting the same error with this:
Code:
(define (script-fu-scale-half image)
    (gimp-image-undo-group-start image)
    (gimp-selection-none image)
    (let*
        (
            (newWidth (car (gimp-image-width image)))
            (newHeight (car (gimp-image-height image)))
        )
        (gimp-image-scale image (/ newWidth 2) (/ newHeight 2))
    )
    (gimp-displays-flush)
    (gimp-image-undo-group-end image)
)

Edit: I wish I knew why, but it inexplicably started working without me changing anything (as far as I can tell). It works now!

Did you refresh the script (or restart Gimp)?
I thought I had (filters>script-fu>refresh scripts), but maybe I had missed it.