From 31397c983d339c5fb5676439ba629bdd3fcde635 Mon Sep 17 00:00:00 2001 From: LaraStuStu Date: Sat, 28 Mar 2020 14:02:25 +0800 Subject: [PATCH] Create image.py --- DataAnnotation/labelme/labelme/utils/image.py | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 DataAnnotation/labelme/labelme/utils/image.py diff --git a/DataAnnotation/labelme/labelme/utils/image.py b/DataAnnotation/labelme/labelme/utils/image.py new file mode 100644 index 0000000..ab7e45d --- /dev/null +++ b/DataAnnotation/labelme/labelme/utils/image.py @@ -0,0 +1,82 @@ +import base64 +import io + +import numpy as np +import PIL.ExifTags +import PIL.Image +import PIL.ImageOps + + +def img_b64_to_arr(img_b64): + f = io.BytesIO() + f.write(base64.b64decode(img_b64)) + img_arr = np.array(PIL.Image.open(f)) + return img_arr + + +def img_arr_to_b64(img_arr): + img_pil = PIL.Image.fromarray(img_arr) + f = io.BytesIO() + img_pil.save(f, format='PNG') + img_bin = f.getvalue() + if hasattr(base64, 'encodebytes'): + img_b64 = base64.encodebytes(img_bin) + else: + img_b64 = base64.encodestring(img_bin) + return img_b64 + + +def img_data_to_png_data(img_data): + with io.BytesIO() as f: + f.write(img_data) + img = PIL.Image.open(f) + + with io.BytesIO() as f: + img.save(f, 'PNG') + f.seek(0) + return f.read() + + +def apply_exif_orientation(image): + try: + exif = image._getexif() + except AttributeError: + exif = None + + if exif is None: + return image + + exif = { + PIL.ExifTags.TAGS[k]: v + for k, v in exif.items() + if k in PIL.ExifTags.TAGS + } + + orientation = exif.get('Orientation', None) + + if orientation == 1: + # do nothing + return image + elif orientation == 2: + # left-to-right mirror + return PIL.ImageOps.mirror(image) + elif orientation == 3: + # rotate 180 + return image.transpose(PIL.Image.ROTATE_180) + elif orientation == 4: + # top-to-bottom mirror + return PIL.ImageOps.flip(image) + elif orientation == 5: + # top-to-left mirror + return PIL.ImageOps.mirror(image.transpose(PIL.Image.ROTATE_270)) + elif orientation == 6: + # rotate 270 + return image.transpose(PIL.Image.ROTATE_270) + elif orientation == 7: + # top-to-right mirror + return PIL.ImageOps.mirror(image.transpose(PIL.Image.ROTATE_90)) + elif orientation == 8: + # rotate 90 + return image.transpose(PIL.Image.ROTATE_90) + else: + return image -- GitLab