Skip to content

Commit

Permalink
Merge pull request #46 from hMatoba/dev_webp
Browse files Browse the repository at this point in the history
Dev webp
  • Loading branch information
hMatoba authored Jan 4, 2018
2 parents 232fb16 + f8ab1ce commit 8677a39
Show file tree
Hide file tree
Showing 16 changed files with 510 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@ load_sample.py
*.suo
doc/_build/
run_coverage.bat
up2pypi.bat
up2pypi.bat
out/
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ python:
- 3.6

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

script:
Expand Down
5 changes: 5 additions & 0 deletions doc/changes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

1.1.0b
------

- "load", "insert", and "remove" support WebP format.

1.0.13
------

Expand Down
10 changes: 10 additions & 0 deletions piexif.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<Folder Include="tests\" />
<Folder Include="piexif\" />
<Folder Include="tests\images\" />
<Folder Include="tests\images\out\" />
</ItemGroup>
<ItemGroup>
<Compile Include="piexif\_common.py" />
Expand All @@ -46,6 +47,9 @@
</Compile>
<Compile Include="piexif\_remove.py" />
<Compile Include="piexif\_transplant.py" />
<Compile Include="piexif\_webp.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="piexif\__init__.py" />
<Compile Include="tests\s_test.py" />
</ItemGroup>
Expand All @@ -57,6 +61,12 @@
<Content Include="tests\images\L02.jpg" />
<Content Include="tests\images\noapp01.jpg" />
<Content Include="tests\images\noexif.jpg" />
<Content Include="tests\images\pil1.webp" />
<Content Include="tests\images\pil2.webp" />
<Content Include="tests\images\pil3.webp" />
<Content Include="tests\images\pil_rgb.webp" />
<Content Include="tests\images\pil_rgba.webp" />
<Content Include="tests\images\tool1.webp" />
</ItemGroup>
<!-- Uncomment the CoreCompile target to enable the Build command in
Visual Studio and specify your pre- and post-build commands in
Expand Down
3 changes: 2 additions & 1 deletion piexif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
from ._exceptions import *


VERSION = '1.0.13'

VERSION = '1.1.0b'
20 changes: 16 additions & 4 deletions piexif/_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from ._common import *
from ._exceptions import InvalidImageDataError

from piexif import _webp

def insert(exif, image, new_file=None):
"""
Expand All @@ -21,14 +21,26 @@ def insert(exif, image, new_file=None):
output_file = False
if image[0:2] == b"\xff\xd8":
image_data = image
file_type = "jpeg"
elif image[0:4] == b"RIFF" and image[8:12] == b"WEBP":
image_data = image
file_type = "webp"
else:
with open(image, 'rb') as f:
image_data = f.read()
if image_data[0:2] != b"\xff\xd8":
if image_data[0:2] == b"\xff\xd8":
file_type = "jpeg"
elif image_data[0:4] == b"RIFF" and image_data[8:12] == b"WEBP":
file_type = "webp"
else:
raise InvalidImageDataError
output_file = True
segments = split_into_segments(image_data)
new_data = merge_segments(segments, exif)

if file_type == "jpeg":
segments = split_into_segments(image_data)
new_data = merge_segments(segments, exif)
elif file_type == "webp":
new_data = _webp.insert(image_data, exif)

if isinstance(new_file, io.BytesIO):
new_file.write(new_data)
Expand Down
20 changes: 13 additions & 7 deletions piexif/_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ._common import *
from ._exceptions import InvalidImageDataError
from ._exif import *

from piexif import _webp

LITTLE_ENDIAN = b"\x49\x49"

Expand Down Expand Up @@ -73,14 +73,13 @@ def __init__(self, data):
self.tiftag = None
elif data[0:2] in (b"\x49\x49", b"\x4d\x4d"): # TIFF
self.tiftag = data
elif data[0:4] == b"RIFF" and data[8:12] == b"WEBP":
self.tiftag = _webp.get_exif(data)
elif data[0:4] == b"Exif": # Exif
self.tiftag = data[6:]
else:
try:
with open(data, 'rb') as f:
magic_number = f.read(2)
except:
raise ValueError("Got invalid value.")
with open(data, 'rb') as f:
magic_number = f.read(2)
if magic_number == b"\xff\xd8": # JPEG
app1 = read_exif_from_file(data)
if app1:
Expand All @@ -91,7 +90,14 @@ def __init__(self, data):
with open(data, 'rb') as f:
self.tiftag = f.read()
else:
raise InvalidImageDataError("Given file is neither JPEG nor TIFF.")
with open(data, 'rb') as f:
header = f.read(12)
if header[0:4] == b"RIFF"and header[8:12] == b"WEBP":
with open(data, 'rb') as f:
file_data = f.read()
self.tiftag = _webp.get_exif(file_data)
else:
raise InvalidImageDataError("Given file is neither JPEG nor TIFF.")

def get_ifd_dict(self, pointer, ifd_name, read_unknown=False):
ifd_dict = {}
Expand Down
33 changes: 25 additions & 8 deletions piexif/_remove.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import io

from ._common import *

from piexif import _webp

def remove(src, new_file=None):
"""
Expand All @@ -14,17 +14,34 @@ def remove(src, new_file=None):
output_is_file = False
if src[0:2] == b"\xff\xd8":
src_data = src
file_type = "jpeg"
elif src[0:4] == b"RIFF" and src[8:12] == b"WEBP":
src_data = src
file_type = "webp"
else:
with open(src, 'rb') as f:
src_data = f.read()
output_is_file = True
segments = split_into_segments(src_data)
exif = get_exif_seg(segments)
if src_data[0:2] == b"\xff\xd8":
file_type = "jpeg"
elif src_data[0:4] == b"RIFF" and src_data[8:12] == b"WEBP":
file_type = "webp"

if exif:
new_data = src_data.replace(exif, b"")
else:
new_data = src_data
if file_type == "jpeg":
segments = split_into_segments(src_data)
exif = get_exif_seg(segments)
if exif:
new_data = src_data.replace(exif, b"")
else:
new_data = src_data
elif file_type == "webp":
try:
new_data = _webp.remove(src_data)
except ValueError:
new_data = src_data
except e:
print(e.args)
raise ValueError("Error ocured.")

if isinstance(new_file, io.BytesIO):
new_file.write(new_data)
Expand All @@ -36,4 +53,4 @@ def remove(src, new_file=None):
with open(src, "wb+") as f:
f.write(new_data)
else:
raise ValueError("Give a 2nd argment to 'remove' to output file")
raise ValueError("Give a second argment to 'remove' to output file")
Loading

0 comments on commit 8677a39

Please sign in to comment.