2019-10-07 16:28:36 +03:00
|
|
|
from io import BytesIO
|
|
|
|
|
2013-07-05 00:57:05 +04:00
|
|
|
from PIL import Image
|
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from .helper import PillowTestCase
|
|
|
|
|
2017-09-28 05:04:24 +03:00
|
|
|
try:
|
|
|
|
from PIL import _webp
|
2019-06-13 18:54:11 +03:00
|
|
|
|
2017-09-28 05:04:24 +03:00
|
|
|
HAVE_WEBP = True
|
|
|
|
except ImportError:
|
|
|
|
HAVE_WEBP = False
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2017-10-02 01:23:18 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestFileWebpMetadata(PillowTestCase):
|
|
|
|
def setUp(self):
|
2017-09-28 05:04:24 +03:00
|
|
|
if not HAVE_WEBP:
|
2019-06-13 18:54:11 +03:00
|
|
|
self.skipTest("WebP support not installed")
|
2014-10-11 20:42:10 +04:00
|
|
|
return
|
|
|
|
|
2017-09-28 05:28:43 +03:00
|
|
|
if not _webp.HAVE_WEBPMUX:
|
2019-06-13 18:54:11 +03:00
|
|
|
self.skipTest("WebPMux support not installed")
|
2014-10-11 20:42:10 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_read_exif_metadata(self):
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2014-06-23 10:19:29 +04:00
|
|
|
file_path = "Tests/images/flower.webp"
|
2014-06-10 13:10:47 +04:00
|
|
|
image = Image.open(file_path)
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(image.format, "WEBP")
|
|
|
|
exif_data = image.info.get("exif", None)
|
|
|
|
self.assertTrue(exif_data)
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
exif = image._getexif()
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# camera make
|
|
|
|
self.assertEqual(exif[271], "Canon")
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
jpeg_image = Image.open("Tests/images/flower.jpg")
|
|
|
|
expected_exif = jpeg_image.info["exif"]
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(exif_data, expected_exif)
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_write_exif_metadata(self):
|
|
|
|
file_path = "Tests/images/flower.jpg"
|
|
|
|
image = Image.open(file_path)
|
2019-06-13 18:54:11 +03:00
|
|
|
expected_exif = image.info["exif"]
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2015-04-24 11:24:52 +03:00
|
|
|
test_buffer = BytesIO()
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2015-04-24 11:24:52 +03:00
|
|
|
image.save(test_buffer, "webp", exif=expected_exif)
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2015-04-24 11:24:52 +03:00
|
|
|
test_buffer.seek(0)
|
|
|
|
webp_image = Image.open(test_buffer)
|
2013-07-05 00:57:05 +04:00
|
|
|
|
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")
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_read_icc_profile(self):
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2014-06-23 10:19:29 +04:00
|
|
|
file_path = "Tests/images/flower2.webp"
|
2014-06-10 13:10:47 +04:00
|
|
|
image = Image.open(file_path)
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(image.format, "WEBP")
|
|
|
|
self.assertTrue(image.info.get("icc_profile", None))
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
icc = image.info["icc_profile"]
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
jpeg_image = Image.open("Tests/images/flower2.jpg")
|
|
|
|
expected_icc = jpeg_image.info["icc_profile"]
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(icc, expected_icc)
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_write_icc_metadata(self):
|
|
|
|
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"]
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2015-04-24 11:24:52 +03:00
|
|
|
test_buffer = BytesIO()
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2015-04-24 11:24:52 +03:00
|
|
|
image.save(test_buffer, "webp", icc_profile=expected_icc_profile)
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2015-04-24 11:24:52 +03:00
|
|
|
test_buffer.seek(0)
|
|
|
|
webp_image = Image.open(test_buffer)
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
webp_icc_profile = webp_image.info.get("icc_profile", None)
|
2013-07-05 00:57:05 +04:00
|
|
|
|
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):
|
|
|
|
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())
|
|
|
|
|
2017-09-27 06:27:40 +03:00
|
|
|
def test_write_animated_metadata(self):
|
2017-09-28 09:10:44 +03:00
|
|
|
if not _webp.HAVE_WEBPANIM:
|
2019-06-13 18:54:11 +03:00
|
|
|
self.skipTest("WebP animation support not available")
|
2017-09-28 09:10:44 +03:00
|
|
|
|
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")
|
2017-09-27 06:27:40 +03:00
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
2017-09-27 06:27:40 +03:00
|
|
|
|
|
|
|
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))
|