Switch to full style
Post all Gimp scripts and script writing questions here
Post a reply

Resize by Area

Fri Dec 14, 2012 2:16 pm

There was a request for a script to resize an image by area:
http://registry.gimp.org/node/27728

The idea being that it makes it easier to compose different proportioned images together so they all have the same visual weight.

I created a script that accomplished this and figured I'd share here as well for testing and suggestions before uploading to the GPR.

It shows up under Image->Resize By Area...

You basically specify an equivalent width and height, and it resizes the image to have the same area as that, using your default scaling method.

(See later in the thread for an update)


-Rob A>

Re: Resize by Area

Fri Dec 14, 2012 3:36 pm

It works like a charm for me, RobA :bigthup
I tested with a variety of image sizes, up and down scale.
This script can be very useful in web design.
win32, Gimp 2.8.2

Re: Resize by Area

Sat Dec 15, 2012 8:01 am

Hi RobA.

I have just tested the script on an image equipped with all the possible objects that can be added to it (layers, layer groups, channels, paths, floating selection), and according to me at the moment it already works great. :)

Howsoever, If I may, I would only like to suggest adding a combo-box widget (SF-OPTION) that allows to choose the interpolation method among the usual ones provided by GIMP, specifying for instance the "Cubic" one as the default.

Also, I think it could be useful to arrange for the script to be available through the "Repeat Last" and "Re-show Last" commands, where such a feature should be easily achieved (I imagine) by setting in the script registration the image type's parameter to "*" rather than to "".

Re: Resize by Area

Mon Dec 17, 2012 4:06 pm

Gino D wrote:... choose the interpolation method among the usual ones provided by GIMP, ...[and]... to be available through the "Repeat Last" and "Re-show Last"


Thanks for the feedback - the image mode as "" was just an oversight, and should have been there.

Good call on the use of SF-ENUM. Added..

here is an update...
please find the actual working one later one.... :)

-Rob A>

Re: Resize by Area

Mon Dec 17, 2012 5:54 pm

RobA in the equation (max 1 (min 262144 (round (* w (sqrt (/ a (* w h)))))))

what does the terms max and min mean? :mrgreen:

Re: Resize by Area

Mon Dec 17, 2012 6:07 pm

RobA wrote:Thanks for the feedback - the image mode as "" was just an oversight, and should have been there.

Good call on the use of SF-ENUM. Added..


Thanks for paying attention to my suggestions. I am glad that you took them into consideration.

Anyway, regarding the updated version, the only thing I don't understand is how the script can set the interpolation method to the appropriate value specified in the dialog box, since in the source code I don't see any "gimp-context-set-interpolation" statement, which I think should be necessary for the purpose. Unless, maybe, the special SF-ENUM option automatically performs this task too (sorry, that's a widget that I am not very familiar with yet).

Re: Resize by Area

Mon Dec 17, 2012 8:35 pm

Gino D to add a choice for interpolation just modify your script to this ↓

Code:
(define (script-fu-resizebyarea img inLayer interpolation inWidth inHeight)
  (let*
    ((w (car (gimp-image-width img)))
    (h (car (gimp-image-height img)))
    (a (* inWidth inHeight))
   )

    ;  it begins here
    (gimp-context-push)
    (gimp-image-undo-group-start img)
   
   (gimp-image-scale-full img
      (max 1 (min 262144 (round (* w (sqrt (/ a (* w h)))))))
      (max 1 (min 262144 (round (* h (sqrt (/ a (* w h)))))))
      interpolation)
      
   (gimp-image-undo-group-end img)
    (gimp-displays-flush)
    (gimp-context-pop)
   
  )
)

(script-fu-register "script-fu-resizebyarea"
                  "<Image>/Image/Resize by Area..."
                    "Resize by Area"
                    "Rob Antonishen"
                    "Rob Antonishen"
                    "Dec 2012"
                    ""
                    SF-IMAGE      "image"      0
                    SF-DRAWABLE   "drawable"   0
               SF-ENUM "Interpolation" '("InterpolationType" "cubic")
               SF-ADJUSTMENT "Equivalent Width" '(1000 1 10000 10 1000 0 1)
               SF-ADJUSTMENT "Equivalent Height" '(1000 1 10000 10 10000 0 1)
)

Re: Resize by Area

Tue Dec 18, 2012 6:27 am

Graechan wrote:
Gino D to add a choice for interpolation just modify your script to this ↓
Code:
(gimp-image-scale-full img
      (max 1 (min 262144 (round (* w (sqrt (/ a (* w h)))))))
      (max 1 (min 262144 (round (* h (sqrt (/ a (* w h)))))))
      interpolation)

Yes, Graechan, the modification you propose to Rob can certainly be a very valid alternative. :)

Nevertheless, as for me and only for being consistent with the last GIMP versions, lately in my scripts and my code snippets I prefer using the generic "gimp-image-scale" procedure preceded by a "gimp-context-interpolation" statement, as "gimp-image-scale-full" is numbered among the commands listed as deprecated in GIMP 2.8 (though I don't quite agree with this decision, finding the latter procedure still very convenient).

Re: Resize by Area

Tue Dec 18, 2012 8:43 am

@ Gino D - Thanks. I'm an idiot. That's what a fellow gets for working on too many things at once! I added the parameter an then never added the code to use it :P
@ Graechan - exactly what is necessary for the 2.6.x series. Also min and max are scheme functions (return the min and return the max). I am using them to clamp the width and height to the allowable limits.

Attached is the updated script that works in 2.6.x (calling interpolate-full) and later versions (setting the context and then calling the default interpolate).
resizebyarea.scm
(2.66 KiB) Downloaded 143 times


This should do the trick for everyone.

-Rob A>

Re: Resize by Area

Tue Dec 18, 2012 2:36 pm

RobA wrote:Attached is the updated script that works in 2.6.x (calling interpolate-full) and later versions (setting the context and then calling the default interpolate).

Well, now your script is perfect, and I find the function checking the gimp version really effective. I too have created a special code snippet with the same purpose for the upcoming new versions of my scripts (one of which, that is "Remove Holes", actually is already available at the GPR.)

Re: Resize by Area

Wed Dec 19, 2012 3:30 pm

Just a last question, Rob: by chance, do you know where it is possible to find an updated list of all the registered enums that the SF-ENUM widget can accept as input?
I have been searching for it everywhere for quite some time, but so far without success. :(

Re: Resize by Area

Wed Dec 19, 2012 6:30 pm

Gino D this is the list I use Gimp-enums

Re: Resize by Area

Thu Dec 20, 2012 4:10 am

Graechan wrote:
Gino D this is the list I use Gimp-enums

Thanks a lot, Graechan. :tyspin
The only problem, as you can see as well, is that unfortunately in such page the specific SF-ENUM widget used by Rob for setting the interpolation method doesn't seem to be listed among the possible registered enums. So that's the reason why I guess there has to be somewhere a more complete list of the enumerators that may be used for the Script-Fu registering, and likely Rob must have got to know about this somehow.

Re: Resize by Area

Thu Dec 20, 2012 7:51 am

Gino D, you should be able to find all of the GIMP enumerated types within http://git.gnome.org/browse/gimp/tree/l ... h=gimp-2-8

Here they are at the time of this posting (with the leading "Gimp" removed):
Code:
AddMaskType BlendMode BrushApplicationMode BrushGeneratedShape BucketFillMode ChannelOps ChannelType CloneType ConvertDitherType ConvertPaletteType ConvolutionType ConvolveType DesaturateMode DodgeBurnType FillType ForegroundExtractMode GradientSegmentColor GradientSegmentType GradientType GridStyle HistogramChannel HueRange IconType ImageBaseType ImageType InkBlobType InterpolationType LayerModeEffects MaskApplyMode MergeType MessageHandlerType OffsetType OrientationType PDBArgType PDBErrorHandler PDBProcType PDBStatusType PaintApplicationMode ProgressCommand RepeatMode RotationType RunMode SelectCriterion SizeType StackTraceMode TextDirection TextHintStyle TextJustification TransferMode TransformDirection TransformResize UserDirectory VectorsStrokeType


You need to be somewhat careful though, because that file is generated dynamically when GIMP is compiled; so the one you are looking at may not exactly match the one that corresponds to your build of GIMP.

Re: Resize by Area

Thu Dec 20, 2012 12:24 pm

saulgoode wrote:Gino D, you should be able to find all of the GIMP enumerated types within http://git.gnome.org/browse/gimp/tree/l ... h=gimp-2-8


Thank you. :)

Re: Resize by Area

Thu Dec 20, 2012 6:03 pm

My apologies Gino D the list I gave is for 2.4 and I only new that Interpolation actually also worked in 2.6 Wilbur is never up to date with info
Post a reply