def _write_openjpeg(self, img_array, verbose=False):
if img_array.ndim == <NUM_LIT:2>:<EOL><INDENT>img_array = img_array.reshape(img_array.shape[<NUM_LIT:0>],<EOL>img_array.shape[<NUM_LIT:1>],<EOL><NUM_LIT:1>)<EOL><DEDENT>self._populate_comptparms(img_array)<EOL>with ExitStack() as stack:<EOL><INDENT>image = opj.image_create(self._comptparms, self._colorspace)<EOL>stack.callback(opj.image_destroy, image)<EOL>numrows, numcols, numlayers = img_array.shape<EOL>image.contents.x0 = self._cparams.image_offset_x0<EOL>image.contents.y0 = self._cparams.image_offset_y0<EOL>image.contents.x1 = (image.contents.x0 +<EOL>(numcols - <NUM_LIT:1>) * self._cparams.subsampling_dx +<EOL><NUM_LIT:1>)<EOL>image.contents.y1 = (image.contents.y0 +<EOL>(numrows - <NUM_LIT:1>) * self._cparams.subsampling_dy +<EOL><NUM_LIT:1>)<EOL>for k in range(<NUM_LIT:0>, numlayers):<EOL><INDENT>layer = np.ascontiguousarray(img_array[:, :, k],<EOL>dtype=np.int32)<EOL>dest = image.contents.comps[k].data<EOL>src = layer.ctypes.data<EOL>ctypes.memmove(dest, src, layer.nbytes)<EOL><DEDENT>cinfo = opj.create_compress(self._cparams.codec_fmt)<EOL>stack.callback(opj.destroy_compress, cinfo)<EOL>event_mgr = opj.EventMgrType()<EOL>_info_handler = _INFO_CALLBACK if verbose else None<EOL>event_mgr.info_handler = _info_handler<EOL>event_mgr.warning_handler = ctypes.cast(_WARNING_CALLBACK,<EOL>ctypes.c_void_p)<EOL>event_mgr.error_handler = ctypes.cast(_ERROR_CALLBACK,<EOL>ctypes.c_void_p)<EOL>opj.setup_encoder(cinfo, ctypes.byref(self._cparams), image)<EOL>cio = opj.cio_open(cinfo)<EOL>stack.callback(opj.cio_close, cio)<EOL>if not opj.encode(cinfo, cio, image):<EOL><INDENT>raise IOError("<STR_LIT>")<EOL><DEDENT>pos = opj.cio_tell(cio)<EOL>blob = ctypes.string_at(cio.contents.buffer, pos)<EOL>fptr = open(self.filename, '<STR_LIT:wb>')<EOL>stack.callback(fptr.close)<EOL>fptr.write(blob)<EOL><DEDENT>self.parse()<EOL>
Write JPEG 2000 file using OpenJPEG 1.5 interface.