Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scale image relative size?
#1
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!
Modder/Skinner at MarvelMods.com using GIMP to create, edit, and export textures and previews more efficiently.

My GIMP scripts hosted on GitHub
Reply
#2
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:

   
Reply
#3
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.


Attached Files
.zip   resize-two-thirds.py.zip (Size: 523 bytes / Downloads: 121)
Reply
#4
(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)
Modder/Skinner at MarvelMods.com using GIMP to create, edit, and export textures and previews more efficiently.

My GIMP scripts hosted on GitHub
Reply
#5
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)))
Reply
#6
(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!
Modder/Skinner at MarvelMods.com using GIMP to create, edit, and export textures and previews more efficiently.

My GIMP scripts hosted on GitHub
Reply
#7
(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)?
Reply
#8
(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.
Modder/Skinner at MarvelMods.com using GIMP to create, edit, and export textures and previews more efficiently.

My GIMP scripts hosted on GitHub
Reply


Forum Jump: