浏览代码

add fix for PyQt4+python 2.7 to avoid window freeze.

When imageData cannot be read with PyQt4, the alert window is shown.
To avoid this, I do conversion from jpg to png (or from other
unsupported format to png).
Tatiana Malygina 6 年之前
父节点
当前提交
fe96adecb8
共有 1 个文件被更改,包括 19 次插入0 次删除
  1. 19 0
      labelme/app.py

+ 19 - 0
labelme/app.py

@@ -1,9 +1,12 @@
 import functools
 import functools
+import io
 import os.path
 import os.path
 import re
 import re
 import warnings
 import warnings
 import webbrowser
 import webbrowser
 
 
+import PIL.Image
+
 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
@@ -936,6 +939,16 @@ class MainWindow(QtWidgets.QMainWindow, WindowMixin):
         for item, shape in self.labelList.itemsToShapes:
         for item, shape in self.labelList.itemsToShapes:
             item.setCheckState(Qt.Checked if value else Qt.Unchecked)
             item.setCheckState(Qt.Checked if value else Qt.Unchecked)
 
 
+    def convertImageDataToPng(self, imageData):
+        if imageData is None:
+            return
+        img = PIL.Image.open(io.BytesIO(imageData))
+        with io.BytesIO() as imgBytesIO:
+            img.save(imgBytesIO, "PNG")
+            imgBytesIO.seek(0)
+            data = imgBytesIO.read()
+        return data
+
     def loadFile(self, filename=None):
     def loadFile(self, filename=None):
         """Load the specified file, or the last opened file if None."""
         """Load the specified file, or the last opened file if None."""
         # changing fileListWidget loads file
         # changing fileListWidget loads file
@@ -965,6 +978,10 @@ class MainWindow(QtWidgets.QMainWindow, WindowMixin):
                 # FIXME: PyQt4 installed via Anaconda fails to load JPEG
                 # FIXME: PyQt4 installed via Anaconda fails to load JPEG
                 # and JSON encoded images.
                 # and JSON encoded images.
                 # https://github.com/ContinuumIO/anaconda-issues/issues/131
                 # 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
+                    self.labelFile.imageData = self.convertImageDataToPng(
+                        self.labelFile.imageData)
                 if QtGui.QImage.fromData(self.labelFile.imageData).isNull():
                 if QtGui.QImage.fromData(self.labelFile.imageData).isNull():
                     raise LabelFileError(
                     raise LabelFileError(
                         'Failed loading image data from label file.\n'
                         'Failed loading image data from label file.\n'
@@ -991,6 +1008,8 @@ class MainWindow(QtWidgets.QMainWindow, WindowMixin):
             if self.imageData is not None:
             if self.imageData is not None:
                 # the filename is image not JSON
                 # the filename is image not JSON
                 self.imagePath = filename
                 self.imagePath = filename
+                if QtGui.QImage.fromData(self.imageData).isNull():
+                    self.imageData = self.convertImageDataToPng(self.imageData)
             self.labelFile = None
             self.labelFile = None
         image = QtGui.QImage.fromData(self.imageData)
         image = QtGui.QImage.fromData(self.imageData)
         if image.isNull():
         if image.isNull():