From 194add490168100e2a68d8669adeb065a2acf256 Mon Sep 17 00:00:00 2001 From: "jim.meyer" Date: Sun, 14 Jun 2020 19:02:11 -0500 Subject: [PATCH 1/2] Fixed unassigned variable error. Also don't both re-writing image file if it hasn't changed. --- piexif/_remove.py | 56 +++++++++++++++++++---------------- tests/images/not_an_image.txt | 1 + tests/s_test.py | 8 +++++ 3 files changed, 39 insertions(+), 26 deletions(-) create mode 100644 tests/images/not_an_image.txt diff --git a/piexif/_remove.py b/piexif/_remove.py index 6ba4911..71ba673 100644 --- a/piexif/_remove.py +++ b/piexif/_remove.py @@ -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") \ No newline at end of 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") \ No newline at end of file diff --git a/tests/images/not_an_image.txt b/tests/images/not_an_image.txt new file mode 100644 index 0000000..dedde5f --- /dev/null +++ b/tests/images/not_an_image.txt @@ -0,0 +1 @@ +This tests _remove() handling of files that are not jpgs or webp \ No newline at end of file diff --git a/tests/s_test.py b/tests/s_test.py index 5d105de..43b7cc5 100644 --- a/tests/s_test.py +++ b/tests/s_test.py @@ -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/" From 7b48d0f2de0961ca7fd36d452f490bc2a97a392a Mon Sep 17 00:00:00 2001 From: "jim.meyer" Date: Sun, 14 Jun 2020 19:05:45 -0500 Subject: [PATCH 2/2] Updated changes.rst and bumped version --- doc/changes.rst | 6 ++++++ piexif/__init__.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/doc/changes.rst b/doc/changes.rst index 12a06f7..4909c3e 100644 --- a/doc/changes.rst +++ b/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 ----- diff --git a/piexif/__init__.py b/piexif/__init__.py index a863445..0f946f3 100644 --- a/piexif/__init__.py +++ b/piexif/__init__.py @@ -8,4 +8,4 @@ -VERSION = '1.1.3' +VERSION = '1.1.4'