11 base64_and_filepath_image_pattern = re.compile(
r'((?:/9j/)(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)? ?|/\S+\.(?:jpeg|jpg|png|gif))')
15 encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), quality]
16 result, encimg = cv2.imencode(
'.jpg', img, encode_param)
17 b64encoded = base64.b64encode(encimg).decode(
'ascii')
22 bin = b64encoded.split(
",")[-1]
23 bin = base64.b64decode(bin)
24 bin = np.frombuffer(bin, np.uint8)
25 img = cv2.imdecode(bin, cv2.IMREAD_COLOR)
32 except Exception
as e:
39 if base64_and_filepath_image_pattern.match(text)
is None:
42 if os.path.exists(text):
44 if imghdr.what(path)
in [
'jpeg',
'jpg',
'png',
'gif']:
45 with open(path,
'rb')
as f:
50 bin = text.split(
",")[-1]
51 bin = base64.b64decode(bin)
52 bin = np.frombuffer(bin, np.uint8)
57 texts = base64_and_filepath_image_pattern.split(text)
58 target_texts = list(filter(
lambda x: x
is not None and len(x.strip()) > 0, texts))
65 for text
in target_texts:
68 split_texts.append(text)
69 imgs_list.append(imgs)
75 imgs_list.append(imgs)
76 if len(split_texts) > 0:
77 if len(split_texts[0]) == 0
and len(imgs_list[0]) == 0:
78 split_texts = split_texts[1:]
79 imgs_list = imgs_list[1:]
80 return imgs_list, split_texts