Skip to content

Commit

Permalink
Merge pull request #31 from jaddison/exif-fixes
Browse files Browse the repository at this point in the history
Explicit error handling fixes
  • Loading branch information
hMatoba authored Jan 29, 2017
2 parents 9fee596 + 63ff686 commit abc4d42
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ python:
- 3.3
- 3.4
- 3.5
- 3.6-dev
- 3.6

install:
- pip install pillow==2.9.0
- pip install pillow==4.0.0
- pip install coveralls

script:
Expand Down
18 changes: 18 additions & 0 deletions doc/appendices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,21 @@ On GoogleAppEngine, it can't save files on disk. Therefore files must be handled

# transplant
piexif.transplant(jpg_data1, jpg_data2, output)

Invalid EXIF Thumbnails
-----------------------

EXIF data will sometimes be either corrupted or written by non-compliant software. When this happens, it's possible
that the thumbnail stored in EXIF cannot be found when attempting to dump the EXIF dictionary.

A good solution would be to remove the thumbnail from the EXIF dictionary and then re-attempt the dump:

::

try:
exif_bytes = piexif.dump(exif_dict)
except InvalidImageDataError:
del exif_dict["1st"]
del exif_dict["thumbnail"]
exif_bytes = piexif.dump(exif_dict)

8 changes: 8 additions & 0 deletions doc/changes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

1.0.12
------

- Added explicit InvalidImageDataError exception to aid users. Related to https://github.com/hMatoba/Piexif/issues/30.
- Fixed minor issue with tests.
- Removed minor amounts of unused logic.
- Updated .travis.yml for Python and Pillow versions.

1.0.11
------

Expand Down
3 changes: 2 additions & 1 deletion piexif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ._transplant import transplant
from ._insert import insert
from ._exif import *
from ._exeptions import *


VERSION = '1.0.11'
VERSION = '1.0.12'
9 changes: 5 additions & 4 deletions piexif/_common.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import struct

from ._exeptions import InvalidImageDataError


def split_into_segments(data):
"""Slices JPEG meta data into a list from JPEG binary data.
"""
if data[0:2] != b"\xff\xd8":
raise ValueError("Given data isn't JPEG.")
raise InvalidImageDataError("Given data isn't JPEG.")

head = 2
segments = [b"\xff\xd8"]
Expand All @@ -21,7 +23,7 @@ def split_into_segments(data):
head = endPoint

if (head >= len(data)):
raise ValueError("Wrong JPEG data.")
raise InvalidImageDataError("Wrong JPEG data.")
return segments

def read_exif_from_file(filename):
Expand All @@ -31,10 +33,9 @@ def read_exif_from_file(filename):
data = f.read(6)

if data[0:2] != b"\xff\xd8":
raise ValueError("Given data isn't JPEG.")
raise InvalidImageDataError("Given data isn't JPEG.")

head = data[2:6]
head_pos = 2
HEAD_LENGTH = 4
exif = None
while 1:
Expand Down
2 changes: 2 additions & 0 deletions piexif/_exeptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class InvalidImageDataError(ValueError):
pass
4 changes: 2 additions & 2 deletions piexif/_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import struct

from ._common import *
from ._exeptions import InvalidImageDataError


def insert(exif, image, new_file=None):
Expand All @@ -24,10 +25,9 @@ def insert(exif, image, new_file=None):
with open(image, 'rb') as f:
image_data = f.read()
if image_data[0:2] != b"\xff\xd8":
raise ValueError
raise InvalidImageDataError
output_file = True
segments = split_into_segments(image_data)
image_exif = get_exif_seg(segments)
new_data = merge_segments(segments, exif)

if isinstance(new_file, io.BytesIO):
Expand Down
3 changes: 2 additions & 1 deletion piexif/_load.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import struct

from ._common import *
from ._exeptions import InvalidImageDataError
from ._exif import *


Expand Down Expand Up @@ -90,7 +91,7 @@ def __init__(self, data):
with open(data, 'rb') as f:
self.tiftag = f.read()
else:
raise ValueError("Given file is neither JPEG nor TIFF.")
raise InvalidImageDataError("Given file is neither JPEG nor TIFF.")

def get_ifd_dict(self, pointer, ifd_name, read_unknown=False):
ifd_dict = {}
Expand Down
1 change: 0 additions & 1 deletion piexif/_transplant.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def transplant(exif_src, image, new_file=None):
image_data = f.read()
output_file = True
segments = split_into_segments(image_data)
image_exif = get_exif_seg(segments)
new_data = merge_segments(segments, exif)

if isinstance(new_file, io.BytesIO):
Expand Down
6 changes: 3 additions & 3 deletions tests/s_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from PIL import Image
import piexif
from piexif import _common, ImageIFD, ExifIFD, GPSIFD, TAGS
from piexif import _common, ImageIFD, ExifIFD, GPSIFD, TAGS, InvalidImageDataError


print("piexif version: {0}".format(piexif.VERSION))
Expand Down Expand Up @@ -596,7 +596,7 @@ def test_ExifReader_return_unknown(self):
b2 = b"\x00\x01" + b"\xff\xff\x00\x00\x00\x00" + b"\x00\x00\x00\x00"
er = piexif._load._ExifReader(b1 + b2)
if er.tiftag[0:2] == b"II":
exifReader.endian_mark = "<"
er.endian_mark = "<"
else:
er.endian_mark = ">"
ifd = er.get_ifd_dict(8, "0th", True)
Expand All @@ -610,7 +610,7 @@ def test_ExifReader_convert_value_fail(self):
er.convert_value((None, None, None, None))

def test_split_into_segments_fail1(self):
with self.assertRaises(ValueError):
with self.assertRaises(InvalidImageDataError):
_common.split_into_segments(b"I'm not JPEG")

def test_split_into_segments_fail2(self):
Expand Down

0 comments on commit abc4d42

Please sign in to comment.