code: def make_CP_encoder(buffers):
      """create an encoder for CellProfiler data and numpy arrays (which will be
      stored in the input argument)"""
  
      def encoder(data, buffers=buffers):
          if isinstance(data, numpy.ndarray):
              #
              # Maybe it's nice to save memory by converting 64-bit to 32-bit
              # but the purpose here is to fix a bug on the Mac where a
              # 32-bit worker gets a 64-bit array or unsigned 32-bit array,
              # tries to use it for indexing and fails because the integer
              # is wider than a 32-bit pointer
              #
              info32 = numpy.iinfo(numpy.int32)
              if (
                  data.dtype.kind == "i"
                  and data.dtype.itemsize > 4
                  or data.dtype.kind == "u"
                  and data.dtype.itemsize >= 4
              ):
                  if numpy.prod(data.shape) == 0 or (
                      numpy.min(data) >= info32.min
                      and numpy.max(data) <= info32.max
                  ):
                      data = data.astype(numpy.int32)
              idx = len(buffers)
              buffers.append(numpy.ascontiguousarray(data))
              dtype = str(data.dtype)
              return {
                  "__ndarray__": True,
                  "dtype": str(data.dtype),
                  "shape": data.shape,
                  "idx": idx,
              }
          if isinstance(data, numpy.generic):
              # http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html
              return data.astype(object)
          if isinstance(data, cellprofiler_core.utilities.grid.Grid):
              d = data.serialize()
              d["__CPGridInfo__"] = True
              return d
          if isinstance(data, memoryview):
              # arbitrary data
              idx = len(buffers)
              buffers.append(data)
              return {"__buffer__": True, "idx": idx}
          raise TypeError(
              "%r of type %r is not JSON serializable"
              % (data, type(data))
          )
  
      return encoder
  
imports: import errno
  import json
  import logging
  import os
  import queue
  import sys
  import threading
  import uuid
  import numpy
  import zmq
  import cellprofiler_core.utilities.grid
  from cellprofiler_core.utilities.zmq._boundary import Boundary
  from cellprofiler_core.utilities.zmq.communicable.reply import LockStatusReply, Reply
  from cellprofiler_core.utilities.zmq.communicable.request import LockStatusRequest, Request
label_desc: Use image node_id 1 for calling a global function with example usage: make_CP_encoder(buffers) and returns: encoder, dict, data, d, dict