Pillow/Tests/test_file_webp_metadata.py

140 lines
3.9 KiB
Python
Raw Normal View History

from PIL import Image
from .helper import PillowTestCase
try:
from PIL import _webp
2019-06-13 18:54:11 +03:00
HAVE_WEBP = True
except ImportError:
HAVE_WEBP = False
2014-06-10 13:10:47 +04:00
class TestFileWebpMetadata(PillowTestCase):
def setUp(self):
if not HAVE_WEBP:
2019-06-13 18:54:11 +03:00
self.skipTest("WebP support not installed")
return
if not _webp.HAVE_WEBPMUX:
2019-06-13 18:54:11 +03:00
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")
2019-06-13 18:54:11 +03: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)
2019-06-13 18:54:11 +03:00
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)
2019-06-13 18:54:11 +03:00
webp_exif = webp_image.info.get("exif", None)
2014-06-10 13:10:47 +04:00
self.assertTrue(webp_exif)
if webp_exif:
2019-06-13 18:54:11 +03:00
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))
2019-06-13 18:54:11 +03:00
icc = image.info["icc_profile"]
2019-06-13 18:54:11 +03: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)
2019-06-13 18:54:11 +03:00
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)
2019-06-13 18:54:11 +03: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(
2019-06-13 18:54:11 +03:00
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)
2019-06-13 18:54:11 +03:00
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):
if not _webp.HAVE_WEBPANIM:
2019-06-13 18:54:11 +03:00
self.skipTest("WebP animation support not available")
2019-06-13 18:54:11 +03:00
iccp_data = "<iccp_data>".encode("utf-8")
exif_data = "<exif_data>".encode("utf-8")
xmp_data = "<xmp_data>".encode("utf-8")
temp_file = self.tempfile("temp.webp")
2019-06-13 18:54:11 +03:00
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)
2019-06-13 18:54:11 +03:00
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))