def test_remove_exif(self): """Can PIL open WebP that is removed exif?""" IMAGE_DIR = "tests/images/" OUT_DIR = "tests/images/out/" files = [ "tool1.webp", "pil1.webp", "pil2.webp", "pil3.webp", "pil_rgb.webp", "pil_rgba.webp", ] for filename in files: try: Image.open(IMAGE_DIR + filename) except: print("Pillow can't read {0}".format(filename)) continue with open(IMAGE_DIR + filename, "rb") as f: data = f.read() exif_removed = _webp.remove(data) with open(OUT_DIR + "r_" + filename, "wb") as f: f.write(exif_removed) Image.open(OUT_DIR + "r_" + filename)
def remove(src, new_file=None): """ py:function:: piexif.remove(filename) Remove exif from JPEG. :param str filename: JPEG """ 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 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 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) 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 argment to 'remove' to output file")
def exif_remove(src): 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: return src 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: new_data = src_data return new_data