Skip to content

Commit

Permalink
Improve test and refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
hMatoba committed Feb 6, 2015
1 parent 71fdb8d commit 5d8dcf5
Show file tree
Hide file tree
Showing 20 changed files with 297 additions and 186 deletions.
Binary file modified doc/_build/doctrees/About.doctree
Binary file not shown.
Binary file modified doc/_build/doctrees/Installation.doctree
Binary file not shown.
Binary file modified doc/_build/doctrees/appendices.doctree
Binary file not shown.
Binary file modified doc/_build/doctrees/changes.doctree
Binary file not shown.
Binary file modified doc/_build/doctrees/environment.pickle
Binary file not shown.
Binary file modified doc/_build/doctrees/functions.doctree
Binary file not shown.
Binary file modified doc/_build/doctrees/index.doctree
Binary file not shown.
Binary file modified doc/_build/doctrees/sample.doctree
Binary file not shown.
1 change: 1 addition & 0 deletions doc/_build/html/About.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="sample.html">Samples</a><ul>
<li class="toctree-l2"><a class="reference internal" href="sample.html#with-pil-pillow">With PIL(Pillow)</a></li>
<li class="toctree-l2"><a class="reference internal" href="sample.html#check-containing-tag">Check Containing Tag</a></li>
<li class="toctree-l2"><a class="reference internal" href="sample.html#rotate-image-by-exif-orientation">Rotate Image by Exif Orientation</a></li>
</ul>
Expand Down
2 changes: 2 additions & 0 deletions doc/_build/html/Installation.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="about.html">About Piexif</a><ul>
<li class="toctree-l2"><a class="reference internal" href="about.html#what-for">What for?</a></li>
<li class="toctree-l2"><a class="reference internal" href="about.html#how-to-use">How to Use</a></li>
<li class="toctree-l2"><a class="reference internal" href="about.html#dependency">Dependency</a></li>
<li class="toctree-l2"><a class="reference internal" href="about.html#environment">Environment</a></li>
<li class="toctree-l2"><a class="reference internal" href="about.html#license">License</a></li>
Expand All @@ -86,6 +87,7 @@
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="sample.html">Samples</a><ul>
<li class="toctree-l2"><a class="reference internal" href="sample.html#with-pil-pillow">With PIL(Pillow)</a></li>
<li class="toctree-l2"><a class="reference internal" href="sample.html#check-containing-tag">Check Containing Tag</a></li>
<li class="toctree-l2"><a class="reference internal" href="sample.html#rotate-image-by-exif-orientation">Rotate Image by Exif Orientation</a></li>
</ul>
Expand Down
2 changes: 2 additions & 0 deletions doc/_build/html/appendices.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="about.html">About Piexif</a><ul>
<li class="toctree-l2"><a class="reference internal" href="about.html#what-for">What for?</a></li>
<li class="toctree-l2"><a class="reference internal" href="about.html#how-to-use">How to Use</a></li>
<li class="toctree-l2"><a class="reference internal" href="about.html#dependency">Dependency</a></li>
<li class="toctree-l2"><a class="reference internal" href="about.html#environment">Environment</a></li>
<li class="toctree-l2"><a class="reference internal" href="about.html#license">License</a></li>
Expand All @@ -86,6 +87,7 @@
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="sample.html">Samples</a><ul>
<li class="toctree-l2"><a class="reference internal" href="sample.html#with-pil-pillow">With PIL(Pillow)</a></li>
<li class="toctree-l2"><a class="reference internal" href="sample.html#check-containing-tag">Check Containing Tag</a></li>
<li class="toctree-l2"><a class="reference internal" href="sample.html#rotate-image-by-exif-orientation">Rotate Image by Exif Orientation</a></li>
</ul>
Expand Down
Binary file modified doc/_build/html/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion doc/_build/html/searchindex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion piexif/_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ._common import *


def insert(exif, image, new_file=""):
def insert(exif, image, new_file=None):
"""
py:function:: piexif.insert(exif_bytes, filename)
Expand Down
194 changes: 94 additions & 100 deletions piexif/_load_and_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, data):
self.tiftag = app1[10:]
else:
self.tiftag = None
elif data[0:2] in (b"\x49\x49", b"\x4d4d"): # TIFF
elif data[0:2] in (b"\x49\x49", b"\x4d\x4d"): # TIFF
self.tiftag = data
elif data[0:4] == b"Exif": # Exif
self.tiftag = data[6:]
Expand Down Expand Up @@ -90,8 +90,8 @@ def get_ifd_dict(self, pointer, ifd_name, read_unknown=False):
ifd_dict[tag] = self.convert_value(v_set)
elif read_unknown:
ifd_dict[tag] = v_set
else:
pass
#else:
# pass

if ifd_name == "0th":
pointer = offset + 12 * tag_count
Expand Down Expand Up @@ -390,9 +390,93 @@ def pack_long(*args):
return struct.pack(">" + "L" * len(args), *args)


def pack_slong(*args):
return struct.pack(">" + "l" * len(args), *args)
#def pack_slong(*args):
# return struct.pack(">" + "l" * len(args), *args)


def value_to_bytes(raw_value, value_type, offset):
four_bytes_over = b""
value_str = b""

if value_type == "Byte":
length = len(raw_value)
if length <= 4:
value_str = (pack_byte(*raw_value) +
b"\x00" * (4 - length))
else:
value_str = struct.pack(">I", offset)
four_bytes_over = pack_byte(*raw_value)
elif value_type == "Short":
length = len(raw_value)
if length <= 2:
value_str = (pack_short(*raw_value) +
b"\x00\x00" * (2 - length))
else:
value_str = struct.pack(">I", offset)
four_bytes_over = pack_short(*raw_value)
elif value_type == "Long":
length = len(raw_value)
if length <= 1:
value_str = pack_long(*raw_value)
else:
value_str = struct.pack(">I", offset)
four_bytes_over = pack_long(*raw_value)
# elif value_type == "SLong":
# length = len(raw_value)
# if length <= 1:
# value_str = pack_long(*raw_value)
# else:
# value_str = struct.pack(">I", offset)
# four_bytes_over = pack_slong(*raw_value)
elif value_type == "Ascii":
try:
new_value = raw_value.encode("latin1") + b"\x00"
except:
new_value = raw_value + b"\x00"
length = len(new_value)
if length > 4:
value_str = struct.pack(">I", offset)
four_bytes_over = new_value
else:
value_str = new_value + b"\x00" * (4 - length)
elif value_type == "Rational":
if isinstance(raw_value[0], NUMBER_TYPE):
length = 1
num, den = raw_value
new_value = struct.pack(">L", num) + struct.pack(">L", den)
elif isinstance(raw_value[0], tuple):
length = len(raw_value)
new_value = b""
for n, val in enumerate(raw_value):
num, den = val
new_value += (struct.pack(">L", num) +
struct.pack(">L", den))
value_str = struct.pack(">I", offset)
four_bytes_over = new_value
elif value_type == "SRational":
if isinstance(raw_value[0], NUMBER_TYPE):
length = 1
num, den = raw_value
new_value = struct.pack(">l", num) + struct.pack(">l", den)
elif isinstance(raw_value[0], tuple):
length = len(raw_value)
new_value = b""
for n, val in enumerate(raw_value):
num, den = val
new_value += (struct.pack(">l", num) +
struct.pack(">l", den))
value_str = struct.pack(">I", offset)
four_bytes_over = new_value
elif value_type == "Undefined":
length = len(raw_value)
if length > 4:
value_str = struct.pack(">I", offset)
four_bytes_over = raw_value
else:
value_str = raw_value + b"\x00" * (4 - length)

length_str = struct.pack(">I", length)
return length_str, value_str, four_bytes_over

def dict_to_bytes(ifd_dict, ifd, ifd_offset):
tag_count = len(ifd_dict)
Expand All @@ -418,101 +502,11 @@ def dict_to_bytes(ifd_dict, ifd, ifd_offset):

if isinstance(raw_value, NUMBER_TYPE):
raw_value = (raw_value,)
offset = TIFF_HEADER_LENGTH + entries_length + ifd_offset + len(values)

if value_type == "Byte":
length = len(raw_value)
if length <= 4:
value_str = (pack_byte(*raw_value) +
b"\x00" * (4 - length))
else:
offset = (TIFF_HEADER_LENGTH + ifd_offset +
entries_length + len(values))
value_str = struct.pack(">I", offset)
four_bytes_over = pack_byte(*raw_value)
elif value_type == "Short":
length = len(raw_value)
if length <= 2:
value_str = (pack_short(*raw_value) +
b"\x00\x00" * (2 - length))
else:
offset = (TIFF_HEADER_LENGTH + ifd_offset +
entries_length + len(values))
value_str = struct.pack(">I", offset)
four_bytes_over = pack_short(*raw_value)
elif value_type == "Long":
length = len(raw_value)
if length <= 1:
value_str = pack_long(*raw_value)
else:
offset = (TIFF_HEADER_LENGTH + ifd_offset +
entries_length + len(values))
value_str = struct.pack(">I", offset)
four_bytes_over = pack_long(*raw_value)
# elif value_type == "SLong":
# length = len(raw_value)
# if length <= 1:
# value_str = pack_long(*raw_value)
# else:
# offset = (TIFF_HEADER_LENGTH + ifd_offset +
# entries_length + len(values))
# value_str = struct.pack(">I", offset)
# four_bytes_over = pack_slong(*raw_value)
elif value_type == "Ascii":
try:
new_value = raw_value.encode("latin1") + b"\x00"
except:
new_value = raw_value + b"\x00"
length = len(new_value)
if length > 4:
offset = (TIFF_HEADER_LENGTH + ifd_offset +
entries_length + len(values))
value_str = struct.pack(">I", offset)
four_bytes_over = new_value
else:
value_str = new_value + b"\x00" * (4 - length)
elif value_type == "Rational":
if isinstance(raw_value[0], NUMBER_TYPE):
length = 1
num, den = raw_value
new_value = struct.pack(">L", num) + struct.pack(">L", den)
elif isinstance(raw_value[0], tuple):
length = len(raw_value)
new_value = b""
for n, val in enumerate(raw_value):
num, den = val
new_value += (struct.pack(">L", num) +
struct.pack(">L", den))
offset = (TIFF_HEADER_LENGTH + ifd_offset +
entries_length + len(values))
value_str = struct.pack(">I", offset)
four_bytes_over = new_value
elif value_type == "SRational":
if isinstance(raw_value[0], NUMBER_TYPE):
length = 1
num, den = raw_value
new_value = struct.pack(">l", num) + struct.pack(">l", den)
elif isinstance(raw_value[0], tuple):
length = len(raw_value)
new_value = b""
for n, val in enumerate(raw_value):
num, den = val
new_value += (struct.pack(">l", num) +
struct.pack(">l", den))
offset = (TIFF_HEADER_LENGTH + ifd_offset +
entries_length + len(values))
value_str = struct.pack(">I", offset)
four_bytes_over = new_value
elif value_type == "Undefined":
length = len(raw_value)
if length > 4:
offset = (TIFF_HEADER_LENGTH + ifd_offset +
entries_length + len(values))
value_str = struct.pack(">I", offset)
four_bytes_over = raw_value
else:
value_str = raw_value + b"\x00" * (4 - length)

length_str = struct.pack(">I", length)
length_str, value_str, four_bytes_over = value_to_bytes(raw_value,
value_type,
offset)
entries += key_str + type_str + length_str + value_str
values += four_bytes_over
return (entry_header + entries, values)
return (entry_header + entries, values)
2 changes: 1 addition & 1 deletion piexif/_transplant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ._common import *


def transplant(exif_src, image, new_file=""):
def transplant(exif_src, image, new_file=None):
"""
py:function:: piexif.transplant(filename1, filename2)
Expand Down
Binary file added tests/images/large.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/images/note.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'r_*.jpg' were some makers' samples that were taken from each official maker's page. They are filled blue to avoid rights, on the other hand their exif are same as original.
Empty file added tests/images/notjpeg.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5d8dcf5

Please sign in to comment.