def execute( self, _to_download_url, _oss_helper: CloudObjectStorage = None, _image_size_threshold=10, ): """ 下载指定url的图像文件 Args: _to_download_url: 待下载的图像url _oss_helper: oss helper用于存储下载好的数据,可以为空 _image_size_threshold: 图像字节数(KB)限制,如果低于阈值会异常 Returns: 下载完成的结果 """ to_return_result = OrderedDict() try: download_result_io = self.download_url(_to_download_url, 1024 * 2) request_image = Image.open(download_result_io) if request_image is None: raise ImageFormatNotSupportException( f'image:{_to_download_url} format not support,cannot decode by PILLOW') image_c = len(request_image.getbands()) image_h = request_image.height image_w = request_image.width request_image_np = convert_pil_to_numpy(request_image) if _image_size_threshold is not None and request_image_np.nbytes < _image_size_threshold * 1024: raise ImageFileSizeAbnormalException( f'image:{_to_download_url} is small than threshold,it may not be a normal picture') # 有些情况是不需要存储的,可能直接就用了。 if _oss_helper: file_name = get_uuid_name() oss_path = os.path.join(get_date_string(), file_name) # 存储原始图像 saved_path = _oss_helper.upload_image_file(self.bucket_name, oss_path, request_image, _enable_compress=False) else: saved_path = '' to_return_result['bucket_name'] = self.bucket_name to_return_result['saved_path'] = saved_path to_return_result['image_height'] = image_h to_return_result['image_width'] = image_w to_return_result['image_channel'] = image_c to_return_result['image'] = request_image_np return to_return_result except requests.exceptions.ConnectionError as connect_error: raise DownloadURLNotAvailableException(f'{_to_download_url} cannot reach') except TimeoutError as te: raise ImageDownloadTimeoutException(f'{_to_download_url} download timeout') except CustomException as ce: raise ce except Exception as e: raise ConsumerAlgorithmUncatchException(tb.format_exc())
def _image_object_decode(_img_object_bytes, _image_size_threshold=10): """ 对图像进行解码 :param _img_object_bytes: 图像字节 :param _image_size_threshold: 图像大小阈值,单位KB :return: 解码后的numpy数组 """ image_file_stream = io.BytesIO(_img_object_bytes) img_pil = Image.open(image_file_stream) request_image = convert_pil_to_numpy(img_pil) if _image_size_threshold and request_image.nbytes < 1024 * _image_size_threshold: raise ImageFileSizeAbnormalException('图像过小,可能不是正常图片') return request_image
def _image_object_decode(_img_object_bytes, _image_size_threshold=10): """ 对图像进行解码 :param _img_object_bytes: 图像字节 :param _image_size_threshold: 图像大小阈值,单位KB :return: 解码后的numpy数组 """ image_file_stream = io.BytesIO(_img_object_bytes) m_image_file_buffer = image_file_stream.read() request_image = cv2.imdecode( np.frombuffer(m_image_file_buffer, np.uint8), -1) if _image_size_threshold and request_image.nbytes < 1024 * _image_size_threshold: raise ImageFileSizeAbnormalException('图像过小,可能不是正常图片') return request_image
def execute( self, _to_download_url, _oss_helper, _timeout=30, _image_size_threshold=10, ): to_return_result = OrderedDict() try: response = requests.get(_to_download_url, timeout=_timeout) data_stream = BytesIO(response.content) m_image_file_buffer = data_stream.read() request_image = cv2.imdecode( np.frombuffer(m_image_file_buffer, np.uint8), -1) if request_image is None: raise ImageFormatNotSupportException( f'image:{_to_download_url} format not support,cannot decode by opencv' ) if len(request_image.shape) == 3: image_h, image_w, image_c = request_image.shape else: image_h, image_w = request_image.shape[:2] image_c = 1 if _image_size_threshold is not None and request_image.nbytes < _image_size_threshold * 1024: raise ImageFileSizeAbnormalException( f'image:{_to_download_url} is small than threshold,it may not be a normal picture' ) file_name = get_uuid_name() oss_path = os.path.join(get_date_string(), file_name) # 存储原始图像 saved_path = _oss_helper.upload_image_file('downloaded-image', oss_path, request_image, _enable_compress=False) to_return_result['bucket_name'] = 'downloaded-image' to_return_result['saved_path'] = saved_path to_return_result['image_height'] = image_h to_return_result['image_width'] = image_w to_return_result['image_channel'] = image_c return to_return_result except requests.exceptions.Timeout as te: raise ImageDownloadTimeoutException( f'{_to_download_url} download timeout') except CustomException as ce: raise ce except Exception as e: raise ConsumerAlgorithmUncatchException(tb.format_exc())