Skip to content

Commit

Permalink
Merge 7b48d0f into 3422fbe
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-meyer committed Jun 15, 2020
2 parents 3422fbe + 7b48d0f commit 758254b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 27 deletions.
6 changes: 6 additions & 0 deletions doc/changes.rst
@@ -1,6 +1,12 @@
Changelog
=========

1.1.4
-----

- Fixed "local variable 'file_type' referenced before assignment" if image type wasn't known.
- No longer write image back to source image file if nothing has changed for better performance.

1.1.3
-----

Expand Down
2 changes: 1 addition & 1 deletion piexif/__init__.py
Expand Up @@ -8,4 +8,4 @@



VERSION = '1.1.3'
VERSION = '1.1.4'
56 changes: 30 additions & 26 deletions piexif/_remove.py
Expand Up @@ -26,31 +26,35 @@ def remove(src, new_file=None):
file_type = "jpeg"
elif src_data[0:4] == b"RIFF" and src_data[8:12] == b"WEBP":
file_type = "webp"

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 occurred.")
file_type = None

if isinstance(new_file, io.BytesIO):
new_file.write(new_data)
new_file.seek(0)
elif new_file:
with open(new_file, "wb+") as f:
f.write(new_data)
elif output_is_file:
with open(src, "wb+") as f:
f.write(new_data)
else:
raise ValueError("Give a second argument to 'remove' to output file")
if file_type is not None:
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 occurred.")

if isinstance(new_file, io.BytesIO):
new_file.write(new_data)
new_file.seek(0)
elif new_file:
with open(new_file, "wb+") as f:
f.write(new_data)
elif output_is_file:
if new_data is not src_data:
with open(src, "wb+") as f:
f.write(new_data)
else:
raise ValueError("Give a second argument to 'remove' to output file")
1 change: 1 addition & 0 deletions tests/images/not_an_image.txt
@@ -0,0 +1 @@
This tests _remove() handling of files that are not jpgs or webp
8 changes: 8 additions & 0 deletions tests/s_test.py
Expand Up @@ -1010,6 +1010,14 @@ def test_remove(self):
piexif.remove(IMAGE_DIR + filename, OUT_DIR + "rr_" + filename)
Image.open(OUT_DIR + "rr_" + filename)

def test_remove_unknown_image_type(self):
"""Does remove ignore unknown image types?"""
IMAGE_DIR = "tests/images/"
OUT_DIR = "tests/images/out/"
filename = "not_an_image.txt"
piexif.remove(IMAGE_DIR + filename, OUT_DIR + "rr_" + filename)
self.assertFalse(os.path.isfile(OUT_DIR + "rr_" + filename))

def test_insert(self):
"""Can PIL open WebP that is inserted exif?"""
IMAGE_DIR = "tests/images/"
Expand Down

0 comments on commit 758254b

Please sign in to comment.