Skip to content

Commit

Permalink
Load JPEG files independent of suffix
Browse files Browse the repository at this point in the history
* cleaned up load()
* added dirty hack to load() (see comments)
* added test for loading tests/images/02.docx (a jpeg file with wrong suffix)
* all tests still working
  • Loading branch information
FelixHaller committed Sep 21, 2016
1 parent 26deea0 commit 113689e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
39 changes: 17 additions & 22 deletions piexif/_load.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import struct

import sys
from ._common import *
from ._exif import *

Expand All @@ -17,13 +17,22 @@ def load(input_data):
:return: Exif data({"0th":dict, "Exif":dict, "GPS":dict, "Interop":dict, "1st":dict, "thumbnail":bytes})
:rtype: dict
"""
exif_dict = {"0th":{},
"Exif":{},
"GPS":{},
"Interop":{},
"1st":{},
"thumbnail":None}
exifReader = _ExifReader(input_data)
exif_dict = {"0th": {},
"Exif": {},
"GPS": {},
"Interop": {},
"1st": {},
"thumbnail": None}
# this is just a hack to make it work.
# The load function (IMHO) has to be redesigned because it has to deal with
# incoming bytes and strings (filenames)
if isinstance(input_data, str):
with open(input_data, 'rb') as f:
data = f.read()
else:
data = input_data

exifReader = _ExifReader(data)
if exifReader.tiftag is None:
return exif_dict

Expand Down Expand Up @@ -71,20 +80,6 @@ def __init__(self, data):
self.tiftag = data
elif data[0:4] == b"Exif": # Exif
self.tiftag = data[6:]
elif data[-4:].lower() in (".jpg", "jpeg", ".jpe", ".tif", "tiff"):
with open(data, 'rb') as f:
data = f.read()
if data[0:2] == b"\xff\xd8": # JPEG
segments = split_into_segments(data)
app1 = get_exif_seg(segments)
if app1:
self.tiftag = app1[10:]
else:
self.tiftag = None
elif data[0:2] in (b"\x49\x49", b"\x4d4d"): # TIFF
self.tiftag = data
else:
raise ValueError("Given file is neither JPEG nor TIFF.")
else:
raise ValueError("Given file is neither JPEG nor TIFF.")

Expand Down
Binary file added tests/images/02.docx
Binary file not shown.
25 changes: 18 additions & 7 deletions tests/s_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
INPUT_FILE2 = os.path.join("tests", "images", "02.jpg")
INPUT_FILE_PEN = os.path.join("tests", "images", "r_pen.jpg")
NOEXIF_FILE = os.path.join("tests", "images", "noexif.jpg")
NODOCX_FILE = os.path.join("tests", "images", "02.docx")
# JPEG without APP0 and APP1 segments
NOAPP01_FILE = os.path.join("tests", "images", "noapp01.jpg")
INPUT_FILE_TIF = os.path.join("tests", "images", "01.tif")
Expand Down Expand Up @@ -89,14 +90,23 @@ class ExifTests(unittest.TestCase):
# load ------
def test_no_exif_load(self):
exif_dict = piexif.load(NOEXIF_FILE)
none_dict = {"0th":{},
"Exif":{},
"GPS":{},
"Interop":{},
"1st":{},
"thumbnail":None}
none_dict = {"0th": {},
"Exif": {},
"GPS": {},
"Interop": {},
"1st": {},
"thumbnail": None}
self.assertEqual(exif_dict, none_dict)

def test_jpg_in_docx_load(self):
"""
Loads an Image with jpg suffix and a copy with docx suffix and compares
exif data.
"""
exif_dict_docx = piexif.load(NODOCX_FILE)
exif_dict_jpg = piexif.load(INPUT_FILE2)
self.assertEqual(exif_dict_docx, exif_dict_jpg)

def test_load(self):
files = glob.glob(os.path.join("tests", "images", "r_*.jpg"))
for input_file in files:
Expand All @@ -113,6 +123,7 @@ def test_load_m(self):
print("********************\n\n" + INPUT_FILE1 + "\n")
self._compare_piexifDict_PILDict(exif, e)


def test_load_tif(self):
exif = piexif.load(INPUT_FILE_TIF)
zeroth_ifd = exif["0th"]
Expand Down Expand Up @@ -148,7 +159,7 @@ def test_load_fail(self):
with self.assertRaises(ValueError):
exif = piexif.load(os.path.join("tests", "images", "notjpeg.jpg"))

with self.assertRaises(ValueError):
with self.assertRaises(FileNotFoundError):
exif = piexif.load("foo")

def test_load_from_pilImage_property(self):
Expand Down

0 comments on commit 113689e

Please sign in to comment.