def transform(self, target_fp, image_request, image_info): fifo_fp = self._make_tmp_fp() mkfifo_call = '%s %s' % (self.mkfifo, fifo_fp) subprocess.check_call(mkfifo_call, shell=True) # kdu command q = '-quiet' t = '-num_threads %s' % self.num_threads i = '-i "%s"' % image_info.src_img_fp o = '-o %s' % fifo_fp reduce_arg = self._scales_to_reduce_arg(image_request, image_info) red = '-reduce %s' % (reduce_arg, ) if reduce_arg else '' region_arg = self._region_to_kdu_arg( image_request.region_param(image_info)) reg = '-region %s' % (region_arg, ) if region_arg else '' kdu_cmd = ' '.join((self.kdu_expand, q, i, t, reg, red, o)) process = multiprocessing.Process(target=self._run_transform, kwargs={ 'target_fp': target_fp, 'image_request': image_request, 'image_info': image_info, 'transform_cmd': kdu_cmd, 'fifo_fp': fifo_fp }) process.start() process.join(self.transform_timeout) if process.is_alive(): logger.info('terminating process for %s, %s', image_info.src_img_fp, target_fp) process.terminate() if path.exists(fifo_fp): unlink(fifo_fp) raise TransformException('Kakadu transform process timed out')
def transform(self, target_fp, image_request, image_info): # opj_decompress command region_arg = self._region_to_opj_arg( image_request.region_param(image_info)) reg = '-d %s' % (region_arg, ) if region_arg else '' reduce_arg = self._scales_to_reduce_arg(image_request, image_info) red = '-r %s' % (reduce_arg, ) if reduce_arg else '' i = '-i %s' % (image_info.src_img_fp, ) with tempfile.TemporaryDirectory(dir=self.tmp_dp) as tmp: tmp_img_fp = os.path.join(tmp, 'image.bmp') o = '-o %s' % (tmp_img_fp, ) opj_cmd = ' '.join((self.opj_decompress, i, reg, red, o)) try: self._process(opj_cmd, target_fp, image_request, image_info, tmp_img_fp) except Exception as e: raise TransformException(f'openjpeg transform error: {e}')
def _process(self, cmd, target_fp, image_request, image_info, fifo_fp): process = multiprocessing.Process(target=self._run_transform, kwargs={ 'target_fp': target_fp, 'image_request': image_request, 'image_info': image_info, 'transform_cmd': cmd, 'fifo_fp': fifo_fp }) process.start() process.join(self.transform_timeout) if process.is_alive(): logger.info('terminating process for %s, %s', image_info.src_img_fp, target_fp) process.terminate() raise TransformException( 'JP2 transform process timed out after %d seconds' % self.transform_timeout)
def transform(self, target_fp, image_request, image_info): # kdu command reduce_arg = self._scales_to_reduce_arg(image_request, image_info) red = '-reduce %s' % (reduce_arg, ) if reduce_arg else '' region_arg = self._region_to_kdu_arg( image_request.region_param(image_info)) reg = '-region %s' % (region_arg, ) if region_arg else '' q = '-quiet' t = '-num_threads %s' % self.num_threads i = '-i %s' % image_info.src_img_fp with tempfile.TemporaryDirectory(dir=self.tmp_dp) as tmp: tmp_img_fp = os.path.join(tmp, 'image.bmp') o = '-o %s' % tmp_img_fp kdu_cmd = ' '.join((self.kdu_expand, q, i, t, reg, red, o)) try: self._process(kdu_cmd, target_fp, image_request, image_info, tmp_img_fp) except Exception as e: raise TransformException(f'kakadu transform error: {e}')
def transform(self, target_fp, image_request, image_info): # opj writes to this: fifo_fp = self._make_tmp_fp() # make the named pipe mkfifo_call = '%s %s' % (self.mkfifo, fifo_fp) resp = subprocess.check_call(mkfifo_call, shell=True) if resp != 0: logger.error('Problem with mkfifo') # how to handle CalledProcessError; would have to be a 500? # opj_decompress command i = '-i "%s"' % (image_info.src_img_fp, ) o = '-o %s' % (fifo_fp, ) region_arg = self._region_to_opj_arg( image_request.region_param(image_info)) reg = '-d %s' % (region_arg, ) if region_arg else '' reduce_arg = self._scales_to_reduce_arg(image_request, image_info) red = '-r %s' % (reduce_arg, ) if reduce_arg else '' opj_cmd = ' '.join((self.opj_decompress, i, reg, red, o)) process = multiprocessing.Process(target=self._run_transform, kwargs={ 'target_fp': target_fp, 'image_request': image_request, 'image_info': image_info, 'transform_cmd': opj_cmd, 'fifo_fp': fifo_fp }) process.start() process.join(self.transform_timeout) if process.is_alive(): logger.info('terminating process for %s, %s', image_info.src_img_fp, target_fp) process.terminate() if path.exists(fifo_fp): unlink(fifo_fp) raise TransformException('OpenJPEG transform process timed out')
def _make_image(self, image_request, image_info): """Call the appropriate transformer to create the image. Args: image_request (ImageRequest) image_info (ImageInfo) Returns: (str) the file path of the new image """ temp_file = NamedTemporaryFile(dir=self.tmp_dp, suffix='.%s' % image_request.format, delete=False) temp_fp = temp_file.name try: transformer = self.transformers[image_info.src_format] transformer.transform(target_fp=temp_fp, image_request=image_request, image_info=image_info) derivative_size = os.stat(temp_fp).st_size if derivative_size < 1: self.logger.error('empty derivative file created for %s' % image_info.src_img_fp) raise TransformException() except Exception: unlink(temp_fp) raise if self.enable_caching: canonical_cache_fp = self.img_cache.upsert( image_request=image_request, temp_fp=temp_fp, image_info=image_info) return canonical_cache_fp else: return temp_fp