INNER CODE UNIT · Python
aspect_ratio
Shilin-LU/TF-ICON · scripts/app.py:43
aspect_ratio = h / w
# Determine the new dimensions for resizing the image while maintaining aspect ratio
if aspect_ratio > 1:
new_w = int(SCALE * 256 / aspect_ratio)
new_h = int(SCALE * 256)
else:
new_w = int(SCALE * 256)
new_h = int(SCALE * 256 * aspect_ratio)
# Resize the image and the segmentation map to the new dimensions
image_resize = image.resize((new_w, new_h))
segmentation_map_resize = cv2.resize(np.array(seg_map).astype(np.uint8), (new_w, new_h), interpolation=cv2.INTER_NEAREST)
# Pad the segmentation map to match the target size
padded_segmentation_map = np.zeros((target_size[1], target_size[0]))
start_x = (target_size[1] - segmentation_map_resize.shape[0]) // 2
start_y = (target_size[0] - segmentation_map_resize.shape[1]) // 2