def load_image(image_bytes): data_stream = io.BytesIO(image_bytes) pil_image = PIL_Image.open(data_stream) if model.pre_concat_frames == -1 and model.pre_concat_frames == -1: pil_image = pil_image.convert('RGB') # if pil_image.mode == 'P': # pil_image = pil_image.convert('RGB') rgb = pil_image.split() size = pil_image.size gif_handle = model.pre_concat_frames != -1 or model.pre_blend_frames != -1 if (len(rgb) > 3 and model.pre_replace_transparent) and not gif_handle: background = PIL_Image.new('RGB', pil_image.size, (255, 255, 255)) background.paste(pil_image, (0, 0, size[0], size[1]), pil_image) pil_image = background if model.pre_concat_frames != -1: im = concat_frames(pil_image, model.pre_concat_frames) elif model.pre_blend_frames != -1: im = blend_frame(pil_image, model.pre_blend_frames) else: im = np.asarray(pil_image) if extract_rgb: im = rgb_filter(im, extract_rgb) im = preprocessing_by_func(exec_map=model.exec_map, key=param_key, src_arr=im) if model.image_channel == 1 and len(im.shape) == 3: im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY) im = preprocessing( image=im, binaryzation=model.pre_binaryzation, ) if model.pre_horizontal_stitching: up_slice = im[0:int(size[1] / 2), 0:size[0]] down_slice = im[int(size[1] / 2):size[1], 0:size[0]] im = np.concatenate((up_slice, down_slice), axis=1) image = im.astype(np.float32) if model.resize[0] == -1: ratio = model.resize[1] / size[1] resize_width = int(ratio * size[0]) image = cv2.resize(image, (resize_width, model.resize[1])) else: image = cv2.resize(image, (model.resize[0], model.resize[1])) image = image.swapaxes(0, 1) return (image[:, :, np.newaxis] if model.image_channel == 1 else image[:, :]) / 255.
def load_image(image_bytes): data_stream = io.BytesIO(image_bytes) pil_image = PIL_Image.open(data_stream) rgb = pil_image.split() size = pil_image.size gif_handle = model.pre_concat_frames != -1 or model.pre_blend_frames != -1 if len(rgb) > 3 and model.pre_replace_transparent and gif_handle: background = PIL_Image.new('RGB', pil_image.size, (255, 255, 255)) background.paste(pil_image, (0, 0, size[0], size[1]), pil_image) pil_image = background if model.pre_concat_frames != -1: im = concat_frames(pil_image, model.pre_concat_frames) elif model.pre_blend_frames != -1: im = blend_frame(pil_image, model.pre_blend_frames) else: im = np.array(pil_image) if model.image_channel == 1 and len(im.shape) == 3: im = im.mean(axis=2).astype(np.float32) im = preprocessing( image=im, binaryzation=model.pre_binaryzation, ) if model.pre_horizontal_stitching: up_slice = im[0:int(size[1] / 2), 0:size[0]] down_slice = im[int(size[1] / 2):size[1], 0:size[0]] im = np.concatenate((up_slice, down_slice), axis=1) image = im.astype(np.float32) if model.resize[0] == -1: ratio = model.resize[1] / size[1] resize_width = int(ratio * size[0]) image = cv2.resize(image, (resize_width, model.resize[1])) else: image = cv2.resize(image, (model.resize[0], model.resize[1])) image = image.swapaxes(0, 1) return (image[:, :, np.newaxis] if model.image_channel == 1 else image[:, :]) / 255.