You are here

Understanding gimp_layer_resize

I am copying a layer, adding it to an image, and shrinking it using gimp_layer_scale.
pdb.gimp_layer_scale( shrunkenLayer, nwidth , nheight , False )
I can center the shrunkenLayer using
shrunkenLayer.set_offsets( (layer0.image.width - nwidth)/2 , (layer0.image.height - nheight)/2 )
This works fine.

If I try to enlarge the shrunken layer to the full image size and center the figure contained in the layer using
wdiff = layer0.image.width-shrunkenLayer.width
hdiff = layer0.image.height-shrunkenLayer.height
pdb.gimp_layer_resize(shrunkenLayer , layer0.image.width , layer0.image.height , wDiff,hDiff)

The resulting figure is full sized (as expected) but not centered. It is at the origin of the image. I have tried +/- wdiff & hdiff but no luck. I thought this function essentially created a new layer and placed the figure from the old layer in it at an offset of wDiff,hDiff from the new layers origin (which I assume is at 0,0.

Can someone help with understand what I've got wrong here.
Thanks.

The offset should be the change from the location of the top,left corner of the layer's new location to the top, left corner of the layer's original location (this seems intuitively backwards, but is trivial to take into account).

The top of a centered layer will be at half the height of the image minus half the height of the layer; similarly for the location of the layer's left side (half the image width minus half the layer width). In short, your values for wdiff and hdiff need to be halved.

You can avoid having to reposition your layer if you initially place it in the upper left corner of the image (i.e., set the layer's offsets to 0,0). You can then resize the layer without needing to calculate the offsets (wdiff=hdiff=0).

Subscribe to Comments for "Understanding gimp_layer_resize"