Pillow/Tests/test_file_webp_metadata.py

133 lines
3.8 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
2015-05-27 02:15:45 +03:00
except ImportError:
2014-06-10 13:10:47 +04:00
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']
2015-04-24 11:24:52 +03:00
test_buffer = BytesIO()
2015-04-24 11:24:52 +03:00
image.save(test_buffer, "webp", exif=expected_exif)
2015-04-24 11:24:52 +03:00
test_buffer.seek(0)
webp_image = Image.open(test_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']
2015-04-24 11:24:52 +03:00
test_buffer = BytesIO()
2015-04-24 11:24:52 +03:00
image.save(test_buffer, "webp", icc_profile=expected_icc_profile)
2015-04-24 11:24:52 +03:00
test_buffer.seek(0)
webp_image = Image.open(test_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)
self.assertIn('exif', image.info)
2014-01-20 22:59:30 +04:00
2015-04-24 11:24:52 +03:00
test_buffer = BytesIO()
2014-01-20 22:59:30 +04:00
2015-04-24 11:24:52 +03:00
image.save(test_buffer, "webp")
2014-01-20 22:59:30 +04:00
2015-04-24 11:24:52 +03:00
test_buffer.seek(0)
webp_image = Image.open(test_buffer)
2014-06-10 13:10:47 +04:00
self.assertFalse(webp_image._getexif())
def test_write_animated_metadata(self):
iccp_data = "<iccp_data>"
exif_data = "<exif_data>"
xmp_data = "<xmp_data>"
temp_file = self.tempfile("temp.webp")
frame1 = Image.open('Tests/images/anim_frame1.webp')
frame2 = Image.open('Tests/images/anim_frame2.webp')
frame1.save(temp_file, save_all=True,
append_images=[frame2, frame1, frame2],
icc_profile=iccp_data, exif=exif_data, xmp=xmp_data)
image = Image.open(temp_file)
self.assertIn('icc_profile', image.info)
self.assertIn('exif', image.info)
self.assertIn('xmp', image.info)
self.assertEqual(iccp_data, image.info.get('icc_profile', None))
self.assertEqual(exif_data, image.info.get('exif', None))
self.assertEqual(xmp_data, image.info.get('xmp', None))
2014-06-10 13:10:47 +04:00
if __name__ == '__main__':
unittest.main()