Pillow/Tests/test_file_webp_metadata.py

116 lines
2.9 KiB
Python
Raw Normal View History

2014-07-07 21:03:50 +04:00
from helper import unittest, PillowTestCase
from PIL import Image
2014-06-10 13:10:47 +04:00
class TestFileWebpMetadata(PillowTestCase):
2014-06-10 13:10:47 +04:00
def setUp(self):
try:
from PIL import _webp
except:
self.skipTest('WebP support not installed')
return
if not _webp.HAVE_WEBPMUX:
self.skipTest('WebPMux support not installed')
2014-06-10 13:10:47 +04:00
def test_read_exif_metadata(self):
file_path = "Tests/images/flower.webp"
2014-06-10 13:10:47 +04:00
image = Image.open(file_path)
2014-06-10 13:10:47 +04:00
self.assertEqual(image.format, "WEBP")
exif_data = image.info.get("exif", None)
self.assertTrue(exif_data)
2014-06-10 13:10:47 +04:00
exif = image._getexif()
2014-06-10 13:10:47 +04:00
# camera make
self.assertEqual(exif[271], "Canon")
2014-06-10 13:10:47 +04:00
jpeg_image = Image.open('Tests/images/flower.jpg')
expected_exif = jpeg_image.info['exif']
2014-06-10 13:10:47 +04:00
self.assertEqual(exif_data, expected_exif)
2014-06-10 13:10:47 +04:00
def test_write_exif_metadata(self):
from io import BytesIO
2014-06-10 13:10:47 +04:00
file_path = "Tests/images/flower.jpg"
image = Image.open(file_path)
expected_exif = image.info['exif']
2014-06-10 13:10:47 +04:00
buffer = BytesIO()
2014-06-10 13:10:47 +04:00
image.save(buffer, "webp", exif=expected_exif)
2014-06-10 13:10:47 +04:00
buffer.seek(0)
webp_image = Image.open(buffer)
2014-06-10 13:10:47 +04:00
webp_exif = webp_image.info.get('exif', None)
self.assertTrue(webp_exif)
if webp_exif:
self.assertEqual(
webp_exif, expected_exif, "WebP EXIF didn't match")
2014-06-10 13:10:47 +04:00
def test_read_icc_profile(self):
file_path = "Tests/images/flower2.webp"
2014-06-10 13:10:47 +04:00
image = Image.open(file_path)
2014-06-10 13:10:47 +04:00
self.assertEqual(image.format, "WEBP")
self.assertTrue(image.info.get("icc_profile", None))
2014-06-10 13:10:47 +04:00
icc = image.info['icc_profile']
2014-06-10 13:10:47 +04:00
jpeg_image = Image.open('Tests/images/flower2.jpg')
expected_icc = jpeg_image.info['icc_profile']
2014-06-10 13:10:47 +04:00
self.assertEqual(icc, expected_icc)
2014-06-10 13:10:47 +04:00
def test_write_icc_metadata(self):
from io import BytesIO
2014-06-10 13:10:47 +04:00
file_path = "Tests/images/flower2.jpg"
image = Image.open(file_path)
expected_icc_profile = image.info['icc_profile']
2014-06-10 13:10:47 +04:00
buffer = BytesIO()
2014-06-10 13:10:47 +04:00
image.save(buffer, "webp", icc_profile=expected_icc_profile)
2014-06-10 13:10:47 +04:00
buffer.seek(0)
webp_image = Image.open(buffer)
2014-06-10 13:10:47 +04:00
webp_icc_profile = webp_image.info.get('icc_profile', None)
2014-06-10 13:10:47 +04:00
self.assertTrue(webp_icc_profile)
if webp_icc_profile:
self.assertEqual(
webp_icc_profile, expected_icc_profile,
"Webp ICC didn't match")
2014-01-20 22:59:30 +04:00
2014-06-10 13:10:47 +04:00
def test_read_no_exif(self):
from io import BytesIO
2014-01-20 22:59:30 +04:00
2014-06-10 13:10:47 +04:00
file_path = "Tests/images/flower.jpg"
image = Image.open(file_path)
image.info['exif']
2014-01-20 22:59:30 +04:00
2014-06-10 13:10:47 +04:00
buffer = BytesIO()
2014-01-20 22:59:30 +04:00
2014-06-10 13:10:47 +04:00
image.save(buffer, "webp")
2014-01-20 22:59:30 +04:00
2014-06-10 13:10:47 +04:00
buffer.seek(0)
webp_image = Image.open(buffer)
self.assertFalse(webp_image._getexif())
if __name__ == '__main__':
unittest.main()
# End of file