Jump to content
Developer Wiki and Function Reference Links ×

Crop imported image


Benny AU

Recommended Posts

Hi all, long time reader first time poster.
 

I'm trying to automate a tedious manual task of importing a number of images to a new sheet layer.

 

I've got the image imports happening using ImportImageFile, however would like to be able to crop them to a standard aspect ratio. Is this possible to script within Vectorworks?

 

The plan was to crop images to a preset size using the centerpoint of the image.

Link to comment

That's a great question.  

 

This almost works.  It puts the image very near 0,0.   But it puts the crop centered not on the center of the image, but rather it centers the crop at the top left of the image.

 

Not sure what to do about this.  I don't see a way to get the size of the image and move the crop.

 

PROCEDURE Test;

CONST
	kPtX			=	0";
	kPtY			=	0";
	RectWidth		=	20';
	RectHeight		=	10';
	
VAR

boolResult				: BOOLEAN;

imageHand, cropHand
						: HANDLE;


PROCEDURE MakeACrop;

BEGIN
	RectangleN( kPtX-RectWidth/2,kPtY-RectHeight/2,
				1,0,
				RectWidth,RectHeight);
	cropHand := LObject;			

END;



BEGIN

	imageHand := ImportImageFile('/Users/MK/Desktop/IMG_2856.jpeg',kPtX,kPtY);

	MakeACrop;

	boolResult := SetImageCropObject(imageHand,cropHand);

END;	





RUN(Test);

 

Link to comment
2 hours ago, Benny AU said:

Hi all, long time reader first time poster.
 

I'm trying to automate a tedious manual task of importing a number of images to a new sheet layer.

 

I've got the image imports happening using ImportImageFile, however would like to be able to crop them to a standard aspect ratio. Is this possible to script within Vectorworks?

 

The plan was to crop images to a preset size using the centerpoint of the image.

What are you wanting to do @Benny AU? Is it contact  / mood sheets / grids of images?

Link to comment

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)

 

  • Like 1
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...