code: def image_encode(args, i, item, q_out):
      """Reads, preprocesses, packs the image and put it back in output queue.
      Parameters
      ----------
      args: object
      i: int
      item: list
      q_out: queue
      """
      fullpath = os.path.join(args.root, item[1])
  
      if len(item) > 3 and args.pack_label:
          header = mx.recordio.IRHeader(0, item[2:], item[0], 0)
      else:
          header = mx.recordio.IRHeader(0, item[2], item[0], 0)
  
      if args.pass_through:
          try:
              with open(fullpath, "rb") as fin:
                  img = fin.read()
              s = mx.recordio.pack(header, img)
              q_out.put((i, s, item))
          except Exception as e:
              traceback.print_exc()
              print("pack_img error:", item[1], e)
              q_out.put((i, None, item))
          return
  
      try:
          img = cv2.imread(fullpath, args.color)
      except:
          traceback.print_exc()
          print(f"imread error trying to load file: {fullpath} ")
          q_out.put((i, None, item))
          return
      if img is None:
          print(f"imread read blank (None) image for file: {fullpath}")
          q_out.put((i, None, item))
          return
      if args.center_crop:
          if img.shape[0] > img.shape[1]:
              margin = (img.shape[0] - img.shape[1]) // 2
              img = img[margin : margin + img.shape[1], :]
          else:
              margin = (img.shape[1] - img.shape[0]) // 2
              img = img[:, margin : margin + img.shape[0]]
      if args.resize:
          if img.shape[0] > img.shape[1]:
              newsize = (
                  args.resize,
                  img.shape[0] * args.resize // img.shape[1],
              )
          else:
              newsize = (
                  img.shape[1] * args.resize // img.shape[0],
                  args.resize,
              )
          img = cv2.resize(img, newsize)
  
      try:
          s = mx.recordio.pack_img(
              header, img, quality=args.quality, img_fmt=args.encoding
          )
          q_out.put((i, s, item))
      except Exception as e:
          traceback.print_exc()
          print(f"pack_img error on file: {fullpath}", e)
          q_out.put((i, None, item))
          return
  
file_path: mxnet/tools/im2rec.py
imports: from __future__ import print_function
  import os
  import sys
  import mxnet
  import random
  import argparse
  import cv2
  import time
  import traceback
label_desc: Use image node_id 5 for calling a global function with example usage: image_encode(args, i, item, q_out) without return types