code: def compute_transformed_contour(
      width, height, fontsize, M, contour, minarea=0.5
  ):
      """Compute the permitted drawing contour
      on a padded canvas for an image of a given size.
      We assume the canvas is padded with one full image width
      and height on left and right, top and bottom respectively.
  
      Args:
          width: Width of image
          height: Height of image
          fontsize: Size of characters
          M: The transformation matrix
          contour: The contour to which we are limited inside
              the rectangle of size width / height
          minarea: The minimum area required for a character
              slot to qualify as being visible, expressed as
              a fraction of the untransformed fontsize x fontsize
              slot.
      """
      spacing = math.ceil(fontsize / 2)
      xslots = int(np.floor(width / spacing))
      yslots = int(np.floor(height / spacing))
      ys, xs = np.mgrid[:yslots, :xslots]
      basis = np.concatenate(
          [xs[..., np.newaxis], ys[..., np.newaxis]], axis=-1
      ).reshape((-1, 2))
      basis *= spacing
      slots_pretransform = np.concatenate(
          [
              (basis + offset)[:, np.newaxis, :]
              for offset in [
                  [0, 0],
                  [spacing, 0],
                  [spacing, spacing],
                  [0, spacing],
              ]
          ],
          axis=1,
      )
      slots = cv2.perspectiveTransform(
          src=slots_pretransform.reshape((1, -1, 2)).astype("float32"),
          m=M,
      )[0]
      inside = (
          np.array(
              [
                  cv2.pointPolygonTest(
                      contour=contour,
                      pt=(int(x), int(y)),
                      measureDist=False,
                  )
                  >= 0
                  for x, y in slots
              ]
          )
          .reshape(-1, 4)
          .all(axis=1)
      )
      slots = slots.reshape(-1, 4, 2)
      areas = (
          np.abs(
              (
                  slots[:, 0, 0] * slots[:, 1, 1]
                  - slots[:, 0, 1] * slots[:, 1, 0]
              )
              + (
                  slots[:, 1, 0] * slots[:, 2, 1]
                  - slots[:, 1, 1] * slots[:, 2, 0]
              )
              + (
                  slots[:, 2, 0] * slots[:, 3, 1]
                  - slots[:, 2, 1] * slots[:, 3, 0]
              )
              + (
                  slots[:, 3, 0] * slots[:, 0, 1]
                  - slots[:, 3, 1] * slots[:, 0, 0]
              )
          )
          / 2
      )
      slots_filtered = slots_pretransform[
          (areas > minarea * spacing * spacing) & inside
      ]
      temporary_image = cv2.drawContours(
          image=np.zeros((height, width), dtype="uint8"),
          contours=slots_filtered,
          contourIdx=-1,
          color=255,
      )
      temporary_image = cv2.dilate(
          src=temporary_image, kernel=np.ones((spacing, spacing))
      )
      newContours, _ = cv2.findContours(
          temporary_image,
          mode=cv2.RETR_TREE,
          method=cv2.CHAIN_APPROX_SIMPLE,
      )
      x, y = slots_filtered[0][0]
      contour = newContours[
          next(
              index
              for index, contour in enumerate(newContours)
              if cv2.pointPolygonTest(
                  contour=contour,
                  pt=(int(x), int(y)),
                  measureDist=False,
              )
              >= 0
          )
      ][:, 0, :]
      return contour
  
imports: import os
  import math
  import glob
  import typing
  import random
  import zipfile
  import string
  import itertools
  import cv2
  import tqdm
  import numpy
  import essential_generators
  import PIL.Image
  import PIL.ImageDraw
  import PIL.ImageFont
  import fontTools.ttLib
  from .None import tools
label_desc: Use image node_id 12 for calling a global function with example usage: compute_transformed_contour(width, height, fontsize, M, contour, minarea) and returns: contour