code: def _write_openjp2(self, img_array, verbose=False):
          
          if img_array.ndim == 2:
              # Force the image to be 3D.  Just makes things easier later on.
              numrows, numcols = img_array.shape
              img_array = img_array.reshape(numrows, numcols, 1)
  
          self._populate_comptparms(img_array)
  
          with ExitStack() as stack:
              image = opj2.image_create(self._comptparms, self._colorspace)
              stack.callback(opj2.image_destroy, image)
  
              self._populate_image_struct(image, img_array)
  
              codec = opj2.create_compress(self._cparams.codec_fmt)
              stack.callback(opj2.destroy_codec, codec)
  
              if self._verbose or verbose:
                  info_handler = _INFO_CALLBACK
              else:
                  info_handler = None
  
              opj2.set_info_handler(codec, info_handler)
              opj2.set_warning_handler(codec, _WARNING_CALLBACK)
              opj2.set_error_handler(codec, _ERROR_CALLBACK)
  
              opj2.setup_encoder(codec, self._cparams, image)
  
              strm = opj2.stream_create_default_file_stream(self.filename,
                                                            False)
              stack.callback(opj2.stream_destroy, strm)
  
              opj2.start_compress(codec, image, strm)
              opj2.encode(codec, strm)
              opj2.end_compress(codec, strm)
  
          # Refresh the metadata.
          self.parse()
docs: Write JPEG 2000 file using OpenJPEG 2.x interface.