Ver código fonte

Move apply_exif_orientation to utils/image.py

Kentaro Wada 6 anos atrás
pai
commit
4f5473c245
3 arquivos alterados com 47 adições e 46 exclusões
  1. 1 46
      labelme/app.py
  2. 1 0
      labelme/utils/__init__.py
  3. 45 0
      labelme/utils/image.py

+ 1 - 46
labelme/app.py

@@ -1207,7 +1207,7 @@ class MainWindow(QtWidgets.QMainWindow):
             return
             return
 
 
         # apply orientation to image according to exif
         # apply orientation to image according to exif
-        image_pil = apply_exif_orientation(image_pil)
+        image_pil = utils.apply_exif_orientation(image_pil)
 
 
         with io.BytesIO() as f:
         with io.BytesIO() as f:
             image_pil.save(f, format='PNG')
             image_pil.save(f, format='PNG')
@@ -1621,48 +1621,3 @@ class MainWindow(QtWidgets.QMainWindow):
                     images.append(relativePath)
                     images.append(relativePath)
         images.sort(key=lambda x: x.lower())
         images.sort(key=lambda x: x.lower())
         return images
         return images
-
-
-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

+ 1 - 0
labelme/utils/__init__.py

@@ -2,6 +2,7 @@
 
 
 from ._io import lblsave
 from ._io import lblsave
 
 
+from .image import apply_exif_orientation
 from .image import img_arr_to_b64
 from .image import img_arr_to_b64
 from .image import img_b64_to_arr
 from .image import img_b64_to_arr
 from .image import img_data_to_png_data
 from .image import img_data_to_png_data

+ 45 - 0
labelme/utils/image.py

@@ -33,3 +33,48 @@ def img_data_to_png_data(img_data):
             img.save(f, 'PNG')
             img.save(f, 'PNG')
             f.seek(0)
             f.seek(0)
             return f.read()
             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