Эх сурвалжийг харах

Fix conversion of imageData to PNG data for PY2 + QT4

Kentaro Wada 6 жил өмнө
parent
commit
1195c45b1e

+ 1 - 0
labelme/__init__.py

@@ -8,6 +8,7 @@ from qtpy import QT_VERSION
 
 __appname__ = 'labelme'
 
+QT4 = QT_VERSION[0] == '4'
 QT5 = QT_VERSION[0] == '5'
 del QT_VERSION
 

+ 0 - 14
labelme/app.py

@@ -1125,20 +1125,6 @@ class MainWindow(QtWidgets.QMainWindow):
                 LabelFile.is_label_file(label_file):
             try:
                 self.labelFile = LabelFile(label_file)
-                # FIXME: PyQt4 installed via Anaconda fails to load JPEG
-                # and JSON encoded images.
-                # https://github.com/ContinuumIO/anaconda-issues/issues/131
-                if QtGui.QImage.fromData(self.labelFile.imageData).isNull():
-                    # tries to read image with PIL and convert it to PNG
-                    if self.labelFile.imageData is not None:
-                        self.labelFile.imageData = utils.img_data_to_png_data(
-                            self.labelFile.imageData
-                        )
-                if QtGui.QImage.fromData(self.labelFile.imageData).isNull():
-                    raise LabelFileError(
-                        'Failed loading image data from label file.\n'
-                        'Maybe this is a known issue of PyQt4 built on'
-                        ' Anaconda, and may be fixed by installing PyQt5.')
             except LabelFileError as e:
                 self.errorMessage(
                     'Error opening file',

+ 10 - 3
labelme/label_file.py

@@ -8,6 +8,7 @@ import PIL.Image
 from labelme._version import __version__
 from labelme.logger import logger
 from labelme import PY2
+from labelme import QT4
 from labelme import utils
 
 
@@ -32,6 +33,7 @@ class LabelFile(object):
         try:
             image_pil = PIL.Image.open(filename)
         except IOError:
+            logger.error('Failed opening image file: {}'.format(filename))
             return
 
         # apply orientation to image according to exif
@@ -39,10 +41,13 @@ class LabelFile(object):
 
         with io.BytesIO() as f:
             ext = osp.splitext(filename)[1].lower()
-            if ext in ['.jpg', '.jpeg']:
-                image_pil.save(f, format='JPEG')
+            if PY2 and QT4:
+                format = 'PNG'
+            elif ext in ['.jpg', '.jpeg']:
+                format = 'JPEG'
             else:
-                image_pil.save(f, format='PNG')
+                format = 'PNG'
+            image_pil.save(f, format=format)
             f.seek(0)
             return f.read()
 
@@ -62,6 +67,8 @@ class LabelFile(object):
                 data = json.load(f)
             if data['imageData'] is not None:
                 imageData = base64.b64decode(data['imageData'])
+                if PY2 and QT4:
+                    imageData = utils.img_data_to_png_data(imageData)
             else:
                 # relative path from label file to relative path from cwd
                 imagePath = osp.join(osp.dirname(filename), data['imagePath'])