code: def scale_boxes(
      img1_shape: tuple[int, ...],
      boxes: torch.Tensor,
      img0_shape: tuple[int, ...],
      ratio_pad: torch.Tensor | None = None,
  ) -> torch.Tensor:
      # Rescale boxes (xyxy) from img1_shape to img0_shape
      if ratio_pad is None:  # calculate from img0_shape
          gain = min(
              img1_shape[0] / img0_shape[0],
              img1_shape[1] / img0_shape[1],
          )  # gain  = old / new
          pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (
              img1_shape[0] - img0_shape[0] * gain
          ) / 2  # wh padding
      else:
          gain = ratio_pad[0][0]
          pad = ratio_pad[1]
  
      boxes[..., [0, 2]] -= pad[0]  # x padding
      boxes[..., [1, 3]] -= pad[1]  # y padding
      boxes[..., :4] /= gain
      clip_boxes(boxes, img0_shape)
      return boxes
  
imports: from __future__ import annotations
  import json
  import os
  import time
  import typing
  from pathlib import Path
  import attr
  import cv2
  import numpy
  import torch
  import torchvision
  from PIL import Image
label_desc: Use image node_id 8 for calling a global function with example usage: scale_boxes(img1_shape, boxes, img0_shape, ratio_pad) and returns: boxes