code: def convert_image(args):
      file_name = args[0]
      source_dir = args[1]
      dest_dir = args[2]
      width = args[3]
      height = args[4]
      mode = args[5]
      ch = args[6]
      num_class = args[7]
      grid_size = args[8]
      anchors = args[9]
  
      src_file_name = os.path.join(source_dir, file_name)
      src_label_file_name = os.path.join(
          source_dir, os.path.splitext(file_name)[0] + ".txt"
      )
      image_file_name = os.path.join(
          dest_dir, "data", os.path.splitext(file_name)[0] + ".png"
      )
      label_file_name = os.path.join(
          dest_dir,
          "data",
          os.path.splitext(file_name)[0] + "_label.csv",
      )
      region_file_name = os.path.join(
          dest_dir,
          "data",
          os.path.splitext(file_name)[0] + "_region.csv",
      )
      try:
          os.makedirs(os.path.dirname(image_file_name))
      except OSError:
          pass  # python2 does not support exists_ok arg
      # print(src_file_name, dest_file_name)
  
      # open source image
      labels = load_label(src_label_file_name)
  
      warp_func = None
      try:
          im = imread(src_file_name)
          if len(im.shape) < 2 or len(im.shape) > 3:
              logger.warning(
                  "Illegal image file format %s.".format(src_file_name)
              )
              raise
          elif len(im.shape) == 3:
              # RGB image
              if im.shape[2] != 3:
                  logger.warning("The image must be RGB or monochrome.")
                  csv_data.remove(data)
                  raise
  
          # resize
          h = im.shape[0]
          w = im.shape[1]
          input_size = (w, h)
          # print(h, w)
          if w != width or h != height:
              # resize image
              if mode == "trimming":
                  # trimming mode
                  if float(h) / w > float(height) / width:
                      target_h = int(float(w) / width * height)
                      # print('crop_target_h', target_h)
                      im = im[
                          (h - target_h) // 2 : h - (h - target_h) // 2,
                          ::,
                      ]
                  else:
                      target_w = int(float(h) / height * width)
                      # print('crop_target_w', target_w)
                      im = im[
                          ::,
                          (w - target_w) // 2 : w - (w - target_w) // 2,
                      ]
                  # print('before', im.shape)
  
                  def trim_warp(label, input_size, output_size):
                      w_scale = input_size[0] * 1.0 / output_size[0]
                      h_scale = input_size[1] * 1.0 / output_size[1]
                      label[0] = (
                          label[0] - (1.0 - 1.0 / w_scale) * 0.5
                      ) * w_scale
                      label[1] = (
                          label[1] - (1.0 - 1.0 / h_scale) * 0.5
                      ) * h_scale
                      label[3] *= w_scale
                      label[4] *= h_scale
                      return label
  
                  warp_func = trim_warp
              elif mode == "padding":
                  # padding mode
                  if float(h) / w < float(height) / width:
                      target_h = int(float(height) / width * w)
                      # print('padding_target_h', target_h)
                      pad = (
                          (
                              (target_h - h) // 2,
                              target_h - (target_h - h) // 2 - h,
                          ),
                          (0, 0),
                      )
                  else:
                      target_w = int(float(width) / height * h)
                      # print('padding_target_w', target_w)
                      pad = (
                          (0, 0),
                          (
                              (target_w - w) // 2,
                              target_w - (target_w - w) // 2 - w,
                          ),
                      )
                  if len(im.shape) == 3:
                      pad = pad + ((0, 0),)
                  im = np.pad(im, pad, "constant")
                  # print('before', im.shape)
  
                  def pad_warp(label, input_size, output_size):
                      w_scale = input_size[0] * 1.0 / output_size[0]
                      h_scale = input_size[1] * 1.0 / output_size[1]
                      label[0] = (
                          label[0] * w_scale + (1.0 - w_scale) * 0.5
                      )
                      label[1] = (
                          label[1] * h_scale + (1.0 - h_scale) * 0.5
                      )
                      label[3] *= w_scale
                      label[4] *= h_scale
                      return label
  
                  warp_func = pad_warp
              im = imresize(im, size=(width, height))
              output_size = (width, height)
              # print('after', im.shape)
  
          # change color ch
          if len(im.shape) == 2 and ch == 3:
              # Monochrome to RGB
              im = np.array([im, im, im]).transpose((1, 2, 0))
          elif len(im.shape) == 3 and ch == 1:
              # RGB to monochrome
              im = np.dot(im[..., :3], [0.299, 0.587, 0.114]).astype(
                  np.uint8
              )
  
          # output image
          imsave(image_file_name, im)
  
      except:
          logger.warning("Failed to convert %s." % (src_file_name))
          raise
  
      # create label and region file
      if warp_func is not None:
          labels = [
              warp_func(label, input_size, output_size)
              for label in labels
          ]
      grid_w = width // grid_size
      grid_h = height // grid_size
      label_array = np.full(
          (len(anchors), grid_h, grid_w), -1, dtype=int
      )
      region_array = np.full(
          (len(anchors), grid_h, grid_w, 4), 0.0, dtype=float
      )
  
      for label in labels:
          label_rect = ObjectRect(XYWH=label[1:]).clip()
  
          if label_rect.width() > 0.0 and label_rect.height() > 0.0:
              gx, gy = int(label_rect.centerx() * grid_w), int(
                  label_rect.centery() * grid_h
              )
              max_iou = 0
              anchor_index = 0
              for i, anchor in enumerate(anchors):
                  anchor_rect = ObjectRect(
                      XYWH=[
                          (gx + 0.5) / grid_w,
                          (gy + 0.5) / grid_h,
                          anchor[0],
                          anchor[1],
                      ]
                  )
                  iou = label_rect.iou(anchor_rect)
                  if iou > max_iou:
                      anchor_index = i
                      max_iou = iou
              label_array[anchor_index][gy][gx] = int(label[0])
              region_array[anchor_index][gy][gx] = [
                  (label_rect.centerx() - anchor_rect.centerx())
                  * grid_w
                  + 0.5,
                  (label_rect.centery() - anchor_rect.centery())
                  * grid_h
                  + 0.5,
                  np.log(label_rect.width() * grid_w),
                  np.log(label_rect.height() * grid_h),
              ]
      np.savetxt(
          label_file_name,
          label_array.reshape(
              (label_array.shape[0] * label_array.shape[1], -1)
          ),
          fmt="%i",
          delimiter=",",
      )
      np.savetxt(
          region_file_name,
          region_array.reshape(
              (region_array.shape[0] * region_array.shape[1], -1)
          ),
          fmt="%f",
          delimiter=",",
      )
  
imports: import csv
  import multiprocessing
  import os
  import re
  import nnabla.logger
  import numpy
  import tqdm
  from nnabla.utils.image_utils import imsave, imread, imresize
label_desc: Use image node_id 2 for calling a global function with example usage: convert_image(args) and returns: label, label