Skip to content

Commit

Permalink
Performance up.
Browse files Browse the repository at this point in the history
  • Loading branch information
hMatoba committed Dec 26, 2016
1 parent d283a43 commit 2455eb1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
6 changes: 6 additions & 0 deletions doc/changes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========


1.0.9
-----

- Performance up "load" jpeg from file.

1.0.8
-----

Expand Down
2 changes: 1 addition & 1 deletion piexif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
from ._exif import *


VERSION = '1.0.8'
VERSION = '1.0.9'
28 changes: 28 additions & 0 deletions piexif/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ def split_into_segments(data):
raise ValueError("Wrong JPEG data.")
return segments

def read_exif_from_file(filename):
"""Slices JPEG meta data into a list from JPEG binary data.
"""
f = open(filename, "rb")
data = f.read(6)

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

head = data[2:6]
head_pos = 2
HEAD_LENGTH = 4
exif = None
while 1:
length = struct.unpack(">H", head[2: 4])[0]

if head[:2] == b"\xff\xe1":
segment_data = f.read(length - 2)
exif = head + segment_data
break
elif head[0:1] == b"\xff":
f.read(length - 2)
head = f.read(HEAD_LENGTH)
else:
break

f.close()
return exif

def get_exif_seg(segments):
"""Returns Exif from JPEG meta data list
Expand Down
12 changes: 6 additions & 6 deletions piexif/_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ def __init__(self, data):
else:
try:
with open(data, 'rb') as f:
data = f.read()
magic_number = f.read(2)
except:
raise ValueError("Got invalid value.")
if data[0:2] == b"\xff\xd8": # JPEG
segments = split_into_segments(data)
app1 = get_exif_seg(segments)
if magic_number == b"\xff\xd8": # JPEG
app1 = read_exif_from_file(data)
if app1:
self.tiftag = app1[10:]
else:
self.tiftag = None
elif data[0:2] in (b"\x49\x49", b"\x4d4d"): # TIFF
self.tiftag = data
elif magic_number in (b"\x49\x49", b"\x4d4d"): # TIFF
with open(data, 'rb') as f:
self.tiftag = f.read()
else:
raise ValueError("Given file is neither JPEG nor TIFF.")

Expand Down

0 comments on commit 2455eb1

Please sign in to comment.