Bläddra i källkod

Load image file with EXIF from both JSON and Image files

Kentaro Wada 6 år sedan
förälder
incheckning
da64647176
2 ändrade filer med 26 tillägg och 26 borttagningar
  1. 3 21
      labelme/app.py
  2. 23 5
      labelme/label_file.py

+ 3 - 21
labelme/app.py

@@ -1,13 +1,9 @@
 import functools
 import functools
-import io
 import os
 import os
 import os.path as osp
 import os.path as osp
 import re
 import re
 import webbrowser
 import webbrowser
 
 
-import PIL.ExifTags
-import PIL.Image
-import PIL.ImageOps
 from qtpy import QtCore
 from qtpy import QtCore
 from qtpy.QtCore import Qt
 from qtpy.QtCore import Qt
 from qtpy import QtGui
 from qtpy import QtGui
@@ -1126,7 +1122,7 @@ class MainWindow(QtWidgets.QMainWindow):
         if self.output_dir:
         if self.output_dir:
             label_file = osp.join(self.output_dir, label_file)
             label_file = osp.join(self.output_dir, label_file)
         if QtCore.QFile.exists(label_file) and \
         if QtCore.QFile.exists(label_file) and \
-                LabelFile.isLabelFile(label_file):
+                LabelFile.is_label_file(label_file):
             try:
             try:
                 self.labelFile = LabelFile(label_file)
                 self.labelFile = LabelFile(label_file)
                 # FIXME: PyQt4 installed via Anaconda fails to load JPEG
                 # FIXME: PyQt4 installed via Anaconda fails to load JPEG
@@ -1162,7 +1158,7 @@ class MainWindow(QtWidgets.QMainWindow):
                 self.fillColor = QtGui.QColor(*self.labelFile.fillColor)
                 self.fillColor = QtGui.QColor(*self.labelFile.fillColor)
             self.otherData = self.labelFile.otherData
             self.otherData = self.labelFile.otherData
         else:
         else:
-            self.imageData = self.loadImageFile(filename)
+            self.imageData = LabelFile.load_image_file(filename)
             if self.imageData:
             if self.imageData:
                 self.imagePath = filename
                 self.imagePath = filename
             self.labelFile = None
             self.labelFile = None
@@ -1200,20 +1196,6 @@ class MainWindow(QtWidgets.QMainWindow):
         self.status("Loaded %s" % osp.basename(str(filename)))
         self.status("Loaded %s" % osp.basename(str(filename)))
         return True
         return True
 
 
-    def loadImageFile(self, filename):
-        try:
-            image_pil = PIL.Image.open(filename)
-        except IOError:
-            return
-
-        # apply orientation to image according to exif
-        image_pil = utils.apply_exif_orientation(image_pil)
-
-        with io.BytesIO() as f:
-            image_pil.save(f, format='PNG')
-            f.seek(0)
-            return f.read()
-
     def resizeEvent(self, event):
     def resizeEvent(self, event):
         if self.canvas and not self.image.isNull()\
         if self.canvas and not self.image.isNull()\
            and self.zoomMode != self.MANUAL_ZOOM:
            and self.zoomMode != self.MANUAL_ZOOM:
@@ -1602,7 +1584,7 @@ class MainWindow(QtWidgets.QMainWindow):
             item = QtWidgets.QListWidgetItem(filename)
             item = QtWidgets.QListWidgetItem(filename)
             item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
             item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
             if QtCore.QFile.exists(label_file) and \
             if QtCore.QFile.exists(label_file) and \
-                    LabelFile.isLabelFile(label_file):
+                    LabelFile.is_label_file(label_file):
                 item.setCheckState(Qt.Checked)
                 item.setCheckState(Qt.Checked)
             else:
             else:
                 item.setCheckState(Qt.Unchecked)
                 item.setCheckState(Qt.Unchecked)

+ 23 - 5
labelme/label_file.py

@@ -1,7 +1,10 @@
 import base64
 import base64
+import io
 import json
 import json
 import os.path
 import os.path
 
 
+import PIL.Image
+
 from labelme._version import __version__
 from labelme._version import __version__
 from labelme.logger import logger
 from labelme.logger import logger
 from labelme import PY2
 from labelme import PY2
@@ -24,6 +27,21 @@ class LabelFile(object):
             self.load(filename)
             self.load(filename)
         self.filename = filename
         self.filename = filename
 
 
+    @staticmethod
+    def load_image_file(filename):
+        try:
+            image_pil = PIL.Image.open(filename)
+        except IOError:
+            return
+
+        # apply orientation to image according to exif
+        image_pil = utils.apply_exif_orientation(image_pil)
+
+        with io.BytesIO() as f:
+            image_pil.save(f, format='PNG')
+            f.seek(0)
+            return f.read()
+
     def load(self, filename):
     def load(self, filename):
         keys = [
         keys = [
             'imageData',
             'imageData',
@@ -42,10 +60,10 @@ class LabelFile(object):
                 imageData = base64.b64decode(data['imageData'])
                 imageData = base64.b64decode(data['imageData'])
             else:
             else:
                 # relative path from label file to relative path from cwd
                 # relative path from label file to relative path from cwd
-                imagePath = os.path.join(os.path.dirname(filename),
-                                         data['imagePath'])
-                with open(imagePath, 'rb') as f:
-                    imageData = f.read()
+                imagePath = os.path.join(
+                    os.path.dirname(filename), data['imagePath']
+                )
+                imageData = self.load_image_file(imagePath)
             flags = data.get('flags')
             flags = data.get('flags')
             imagePath = data['imagePath']
             imagePath = data['imagePath']
             self._check_image_height_and_width(
             self._check_image_height_and_width(
@@ -143,5 +161,5 @@ class LabelFile(object):
             raise LabelFileError(e)
             raise LabelFileError(e)
 
 
     @staticmethod
     @staticmethod
-    def isLabelFile(filename):
+    def is_label_file(filename):
         return os.path.splitext(filename)[1].lower() == LabelFile.suffix
         return os.path.splitext(filename)[1].lower() == LabelFile.suffix