diff --git a/labelme/__init__.py b/labelme/__init__.py index e07a8b00e..6b50d0055 100644 --- a/labelme/__init__.py +++ b/labelme/__init__.py @@ -8,6 +8,7 @@ __appname__ = 'labelme' +QT4 = QT_VERSION[0] == '4' QT5 = QT_VERSION[0] == '5' del QT_VERSION diff --git a/labelme/app.py b/labelme/app.py index 53bc23f25..3625154cc 100644 --- a/labelme/app.py +++ b/labelme/app.py @@ -1125,20 +1125,6 @@ def loadFile(self, filename=None): 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', diff --git a/labelme/label_file.py b/labelme/label_file.py index e66a32b07..a6e90a105 100644 --- a/labelme/label_file.py +++ b/labelme/label_file.py @@ -8,6 +8,7 @@ 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 @@ def load_image_file(filename): 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 @@ def load_image_file(filename): 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 @@ def load(self, filename): 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'])