def __init__(self, data): # Prevents "UnicodeWarning: Unicode equal comparison failed" warnings on Python 2 maybe_image = sys.version_info >= (3, 0, 0) or isinstance(data, str) if maybe_image and data[0:2] == b"\xff\xd8": # JPEG segments = split_into_segments(data) app1 = get_exif_seg(segments) if app1: self.tiftag = app1[10:] else: self.tiftag = None elif maybe_image and data[0:2] in (b"\x49\x49", b"\x4d\x4d"): # TIFF self.tiftag = data elif maybe_image and data[0:4] == b"RIFF" and data[8:12] == b"WEBP": self.tiftag = _webp.get_exif(data) elif maybe_image and data[0:4] == b"Exif": # Exif self.tiftag = data[6:] else: 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: self.tiftag = app1[10:] else: self.tiftag = None elif magic_number in (b"\x49\x49", b"\x4d\x4d"): # TIFF with open(data, 'rb') as f: self.tiftag = f.read() else: 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 __init__(self, data): if data[0:2] == b"\xff\xd8": # JPEG segments = split_into_segments(data) app1 = get_exif_seg(segments) if app1: self.tiftag = app1[10:] else: 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: 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: self.tiftag = app1[10:] else: self.tiftag = None elif magic_number in (b"\x49\x49", b"\x4d\x4d"): # TIFF with open(data, 'rb') as f: self.tiftag = f.read() else: 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 test_get_exif(self): """Can we get exif from WebP?""" IMAGE_DIR = "tests/images/" OUT_DIR = "tests/images/out/" files = [ "tool1.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_bytes = _webp.get_exif(data) self.assertEqual(exif_bytes[0:2], b"MM")