Untitled
unknown
plain_text
2 years ago
1.9 kB
8
Indexable
Copy code import numpy as np def scale_polygons(polygons, original_size, new_size): """ Scales a list of polygons to a new size while preserving the aspect ratio. Parameters: - polygons: a list of polygons, where each polygon is a list of points - original_size: the size of the original image as a tuple (height, width) - new_size: the desired size of the new image as a tuple (height, width) Returns: - a list of scaled polygons """ # Calculate the aspect ratios of the original and new images original_aspect_ratio = original_size[1] / original_size[0] new_aspect_ratio = new_size[1] / new_size[0] # Calculate the scale factor based on the aspect ratio that changes the least if original_aspect_ratio < new_aspect_ratio: scale_factor = new_size[0] / original_size[0] else: scale_factor = new_size[1] / original_size[1] # Create an empty list to store the scaled polygons scaled_polygons = [] # Iterate through each polygon in the list for polygon in polygons: # Create an empty list to store the scaled points of the polygon scaled_polygon = [] # Iterate through each point in the polygon for point in polygon: # Scale the point using the scale factor and the center of the image center_x = original_size[1] / 2 center_y = original_size[0] / 2 scaled_x = (point[0] - center_x) * scale_factor + new_size[1] / 2 scaled_y = (point[1] - center_y) * scale_factor + new_size[0] / 2 scaled_point = [int(scaled_x), int(scaled_y)] # Append the scaled point to the scaled polygon scaled_polygon.append(scaled_point) # Append the scaled polygon to the list of scaled polygons scaled_polygons.append(scaled_polygon) return scaled_polygons
Editor is loading...