(self, image, image_process_mode, return_pil=False, image_format='PNG', max_len=1344, min_len=672)
| 110 | self.messages.append([role, message]) |
| 111 | |
| 112 | def process_image(self, image, image_process_mode, return_pil=False, image_format='PNG', max_len=1344, min_len=672): |
| 113 | if image_process_mode == "Pad": |
| 114 | def expand2square(pil_img, background_color=(122, 116, 104)): |
| 115 | width, height = pil_img.size |
| 116 | if width == height: |
| 117 | return pil_img |
| 118 | elif width > height: |
| 119 | result = Image.new(pil_img.mode, (width, width), background_color) |
| 120 | result.paste(pil_img, (0, (width - height) // 2)) |
| 121 | return result |
| 122 | else: |
| 123 | result = Image.new(pil_img.mode, (height, height), background_color) |
| 124 | result.paste(pil_img, ((height - width) // 2, 0)) |
| 125 | return result |
| 126 | image = expand2square(image) |
| 127 | elif image_process_mode in ["Default", "Crop"]: |
| 128 | pass |
| 129 | elif image_process_mode == "Resize": |
| 130 | image = image.resize((336, 336)) |
| 131 | else: |
| 132 | raise ValueError(f"Invalid image_process_mode: {image_process_mode}") |
| 133 | if max(image.size) > max_len: |
| 134 | max_hw, min_hw = max(image.size), min(image.size) |
| 135 | aspect_ratio = max_hw / min_hw |
| 136 | shortest_edge = int(min(max_len / aspect_ratio, min_len, min_hw)) |
| 137 | longest_edge = int(shortest_edge * aspect_ratio) |
| 138 | W, H = image.size |
| 139 | if H > W: |
| 140 | H, W = longest_edge, shortest_edge |
| 141 | else: |
| 142 | H, W = shortest_edge, longest_edge |
| 143 | image = image.resize((W, H)) |
| 144 | if return_pil: |
| 145 | return image |
| 146 | else: |
| 147 | buffered = BytesIO() |
| 148 | image.save(buffered, format=image_format) |
| 149 | img_b64_str = base64.b64encode(buffered.getvalue()).decode() |
| 150 | return img_b64_str |
| 151 | |
| 152 | def get_images(self, return_pil=False): |
| 153 | images = [] |
no test coverage detected