- Duplicate layer
- Autocrop copy
- Figure out the size and offsets of a potential square crop of the layer from the cropped width, height and offsets.
- remove copy
- crop the original layer using gimp-layer-resize
Something like this:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import os, sys
from gimpfu import *
def squareCrop(image,layer):
pdb.gimp_image_undo_group_start(image)
try:
x0,y0=layer.offsets
# get size and offsets of fully autocropped layer
copy=layer.copy()
image.add_layer(copy)
pdb.plug_in_autocrop_layer(image,layer)
xC,yC=copy.offsets
hC,wC=copy.height,copy.width
image.remove_layer(copy)
# determine padding direction
topBottomPad=wC > hC
pad=abs(hC-wC)/2
# determine size and offsets for resize
xOff,yOff=(x0-xC,y0+pad-yC) if topBottomPad else (x0+pad-xC,y0-yC)
w,h=(wC,wC) if topBottomPad else (hC,hC)
layer.resize(w,h,xOff,yOff)
except Exception as e:
print e.args[0]
pdb.gimp_message(e.args[0])
pdb.gimp_image_undo_group_end(image)
### Registrations
whoiam='\n'+os.path.abspath(sys.argv[0])
register(
'square-crop',
'Square crop'+whoiam,
'Square crop',
'Ofnuts','Ofnuts','2016',
'Square crop...',
"RGB*",
[
(PF_IMAGE, "image", "Input image", None),
(PF_DRAWABLE, "drawable", "Input drawable", None),
],
[],
squareCrop,
menu="<Image>/Layer/Transform",
)
main()
Note you cannot crop that way if you are too close to a border, because the padding would extend beyond the canvas limits (something which isn't checked by the code above)