Amazing, thanks @michaelk. Just the push in the right direction I needed!
The below is working for me. I did switch over to Python. Apologies, I know it's not the subject of this forum but it's a bit more familiar to me! Hopefully the framework and methods can help someone else.
The code below inserts an image at any point, resizes it to match the target (scaled using the smaller edge of the imported image), then crops it to match the target size. Initially I tried to crop it to the target aspect ratio and then resize it, but found using SetBBox after SetImageCropObject messed with the crop.
@unearthed I'm importing images which can be of any size or shape from a directory into a sheet layer in a grid where all images are the same size and shape.
import vs
TARGET_WIDTH = 130
TARGET_HEIGHT = 118
image_centre_pt = (-100,50)
vs.ImportImageFile("/Users/b/Documents/tall tree.jpg", image_centre_pt)
img_handle = vs.LActLayer()
### STEP 1 get image info ###
imported_img_bounding_box = vs.GetBBox(img_handle)
x1 = imported_img_bounding_box[0][0]
y1 = imported_img_bounding_box[0][1]
x2 = imported_img_bounding_box[1][0]
y2 = imported_img_bounding_box[1][1]
imported_width = x2 - x1
imported_height = y1 - y2
### STEP 2 resize image using smaller edge to scale ###
if (TARGET_WIDTH / TARGET_HEIGHT) > (imported_width / imported_height):
#imported image is too tall
width_resize_delta = (TARGET_WIDTH - imported_width) / 2
height_resize_delta = (TARGET_WIDTH / imported_width * imported_height - imported_height) / 2
else:
#imported image is too wide
width_resize_delta = (TARGET_HEIGHT / imported_height * imported_width - imported_width) / 2
height_resize_delta = (TARGET_HEIGHT - imported_height) / 2
# make adjustment to top & bottom, left & right to maintain centre point
x1 -= width_resize_delta
x2 += width_resize_delta
y1 += height_resize_delta
y2 -= height_resize_delta
vs.SetBBox(img_handle, (x1, y1), (x2, y2))
### STEP 3 crop to target size ###
image_bounding_box_points = vs.GetBBox(img_handle)
resized_width = image_bounding_box_points[1][0] - image_bounding_box_points[0][0]
resized_height = image_bounding_box_points[0][1] - image_bounding_box_points[1][1]
# origin (0,0) puts the bottom left corner of the crop rect
# at the top left corner of the image. Offset to centre on img
x_crop_offset = (resized_width - TARGET_WIDTH) / 2
y_crop_offset = - (resized_height - TARGET_HEIGHT) / 2 - TARGET_HEIGHT
crop_origin = (x_crop_offset, y_crop_offset)
vs.RectangleN(crop_origin, (1, 0), TARGET_WIDTH, TARGET_HEIGHT)
rect_handle = vs.LActLayer()
vs.SetImageCropObject(img_handle, rect_handle)