|
1 | | -import io |
2 | 1 | import logging |
3 | 2 | import os |
4 | 3 | from typing import List, Tuple, Optional |
5 | 4 |
|
6 | 5 | import numpy as np |
7 | | -from PIL import Image |
8 | 6 | from wai.common.adams.imaging.locateobjects import LocatedObject, LocatedObjects |
9 | 7 | from wai.common.geometry import Point as WaiPoint, Polygon as WaiPolygon |
10 | 8 |
|
11 | 9 | from idc.api import ImageSegmentationAnnotations, ImageClassificationData, ImageSegmentationData, ObjectDetectionData, \ |
12 | | - ImageData, crop_image, pad_image, fit_layers, fit_located_object |
| 10 | + ImageData, crop_image, pad_image, fit_layers, fit_located_object, array_to_image, empty_image |
| 11 | +import logging |
| 12 | +import os |
| 13 | +from typing import List, Tuple, Optional |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +from wai.common.adams.imaging.locateobjects import LocatedObject, LocatedObjects |
| 17 | +from wai.common.geometry import Point as WaiPoint, Polygon as WaiPolygon |
| 18 | + |
| 19 | +from idc.api import ImageSegmentationAnnotations, ImageClassificationData, ImageSegmentationData, ObjectDetectionData, \ |
| 20 | + ImageData, crop_image, pad_image, fit_layers, fit_located_object, array_to_image, empty_image |
13 | 21 |
|
14 | 22 | REGION_SORTING_NONE = "none" |
15 | 23 | REGION_SORTING_XY = "x-then-y" |
@@ -162,8 +170,7 @@ def process_image(item: ImageData, regions_lobj: List[LocatedObject], regions_xy |
162 | 170 | sub_image = pil.crop((x0, y0, x1+1, y1+1)) |
163 | 171 | orig_dims = LocatedObject(0, 0, sub_image.size[0], sub_image.size[1]) |
164 | 172 | sub_image = pad_image(sub_image, pad_width=pad_width, pad_height=pad_height) |
165 | | - sub_bytes = io.BytesIO() |
166 | | - sub_image.save(sub_bytes, format=item.image_format) |
| 173 | + _, sub_bytes = array_to_image(sub_image, item.image_format) |
167 | 174 | image_name_new = region_filename(item.image_name, regions_lobj, regions_xyxy, region_index, suffix) |
168 | 175 |
|
169 | 176 | # crop annotations and forward |
@@ -214,9 +221,7 @@ def new_from_template(item, rebuild_image: bool = False): |
214 | 221 | :return: the new container |
215 | 222 | """ |
216 | 223 | if rebuild_image: |
217 | | - img = Image.new(item.image.mode, item.image_size) |
218 | | - img_bytes = io.BytesIO() |
219 | | - img.save(img_bytes, format=item.image_format) |
| 224 | + _, img_bytes = empty_image(item.image.mode, item.image_width, item.image_height, item.image_format) |
220 | 225 | data = img_bytes.getvalue() |
221 | 226 | else: |
222 | 227 | data = item.image_bytes |
|
0 commit comments