Skip to content

Commit

Permalink
Load image file with EXIF from both JSON and Image files
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Mar 16, 2019
1 parent aedd590 commit da64647
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
24 changes: 3 additions & 21 deletions labelme/app.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import functools
import io
import os
import os.path as osp
import re
import webbrowser

import PIL.ExifTags
import PIL.Image
import PIL.ImageOps
from qtpy import QtCore
from qtpy.QtCore import Qt
from qtpy import QtGui
Expand Down Expand Up @@ -1126,7 +1122,7 @@ def loadFile(self, filename=None):
if self.output_dir:
label_file = osp.join(self.output_dir, label_file)
if QtCore.QFile.exists(label_file) and \
LabelFile.isLabelFile(label_file):
LabelFile.is_label_file(label_file):
try:
self.labelFile = LabelFile(label_file)
# FIXME: PyQt4 installed via Anaconda fails to load JPEG
Expand Down Expand Up @@ -1162,7 +1158,7 @@ def loadFile(self, filename=None):
self.fillColor = QtGui.QColor(*self.labelFile.fillColor)
self.otherData = self.labelFile.otherData
else:
self.imageData = self.loadImageFile(filename)
self.imageData = LabelFile.load_image_file(filename)
if self.imageData:
self.imagePath = filename
self.labelFile = None
Expand Down Expand Up @@ -1200,20 +1196,6 @@ def loadFile(self, filename=None):
self.status("Loaded %s" % osp.basename(str(filename)))
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):
if self.canvas and not self.image.isNull()\
and self.zoomMode != self.MANUAL_ZOOM:
Expand Down Expand Up @@ -1602,7 +1584,7 @@ def importDirImages(self, dirpath, pattern=None, load=True):
item = QtWidgets.QListWidgetItem(filename)
item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
if QtCore.QFile.exists(label_file) and \
LabelFile.isLabelFile(label_file):
LabelFile.is_label_file(label_file):
item.setCheckState(Qt.Checked)
else:
item.setCheckState(Qt.Unchecked)
Expand Down
28 changes: 23 additions & 5 deletions labelme/label_file.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import base64
import io
import json
import os.path

import PIL.Image

from labelme._version import __version__
from labelme.logger import logger
from labelme import PY2
Expand All @@ -24,6 +27,21 @@ def __init__(self, filename=None):
self.load(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):
keys = [
'imageData',
Expand All @@ -42,10 +60,10 @@ def load(self, filename):
imageData = base64.b64decode(data['imageData'])
else:
# 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')
imagePath = data['imagePath']
self._check_image_height_and_width(
Expand Down Expand Up @@ -143,5 +161,5 @@ def save(
raise LabelFileError(e)

@staticmethod
def isLabelFile(filename):
def is_label_file(filename):
return os.path.splitext(filename)[1].lower() == LabelFile.suffix

0 comments on commit da64647

Please sign in to comment.