Skip to content

Commit

Permalink
Fix conversion of imageData to PNG data for PY2 + QT4
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Mar 16, 2019
1 parent 2dfad85 commit 1195c45
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
1 change: 1 addition & 0 deletions labelme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

__appname__ = 'labelme'

QT4 = QT_VERSION[0] == '4'
QT5 = QT_VERSION[0] == '5'
del QT_VERSION

Expand Down
14 changes: 0 additions & 14 deletions labelme/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 10 additions & 3 deletions labelme/label_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -32,17 +33,21 @@ 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
image_pil = utils.apply_exif_orientation(image_pil)

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()

Expand All @@ -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'])
Expand Down

0 comments on commit 1195c45

Please sign in to comment.