Pillow/Tests/test_file_ico.py

103 lines
3.4 KiB
Python
Raw Normal View History

from .helper import PillowTestCase, hopper
2012-10-26 02:00:19 +04:00
2014-11-08 09:49:50 +03:00
import io
2019-05-29 14:05:45 +03:00
from PIL import Image, ImageDraw, IcoImagePlugin
2012-10-26 02:00:19 +04:00
TEST_ICO_FILE = "Tests/images/hopper.ico"
2012-10-26 02:00:19 +04:00
2014-06-10 13:10:47 +04:00
class TestFileIco(PillowTestCase):
def test_sanity(self):
im = Image.open(TEST_ICO_FILE)
2014-06-10 13:10:47 +04:00
im.load()
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.size, (16, 16))
self.assertEqual(im.format, "ICO")
2019-03-04 07:02:47 +03:00
self.assertEqual(im.get_format_mimetype(), "image/x-icon")
2014-06-10 13:10:47 +04:00
2015-07-03 08:03:25 +03:00
def test_invalid_file(self):
with open("Tests/images/flower.jpg", "rb") as fp:
2019-06-13 18:54:11 +03:00
self.assertRaises(SyntaxError, IcoImagePlugin.IcoImageFile, fp)
2015-07-03 08:03:25 +03:00
2014-11-08 09:49:50 +03:00
def test_save_to_bytes(self):
output = io.BytesIO()
im = hopper()
im.save(output, "ico", sizes=[(32, 32), (64, 64)])
2014-11-13 09:45:35 +03:00
# the default image
2014-11-08 09:49:50 +03:00
output.seek(0)
reloaded = Image.open(output)
2019-06-13 18:54:11 +03:00
self.assertEqual(reloaded.info["sizes"], {(32, 32), (64, 64)})
2014-11-08 09:49:50 +03:00
self.assertEqual(im.mode, reloaded.mode)
self.assertEqual((64, 64), reloaded.size)
self.assertEqual(reloaded.format, "ICO")
2019-06-13 18:54:11 +03:00
self.assert_image_equal(reloaded, hopper().resize((64, 64), Image.LANCZOS))
2014-11-08 09:49:50 +03:00
2014-11-13 09:45:35 +03:00
# the other one
output.seek(0)
reloaded = Image.open(output)
2015-04-24 02:26:52 +03:00
reloaded.size = (32, 32)
2014-11-13 09:45:35 +03:00
self.assertEqual(im.mode, reloaded.mode)
self.assertEqual((32, 32), reloaded.size)
self.assertEqual(reloaded.format, "ICO")
2019-06-13 18:54:11 +03:00
self.assert_image_equal(reloaded, hopper().resize((32, 32), Image.LANCZOS))
2018-09-30 07:47:05 +03:00
def test_incorrect_size(self):
im = Image.open(TEST_ICO_FILE)
with self.assertRaises(ValueError):
im.size = (1, 1)
2016-11-30 15:37:50 +03:00
def test_save_256x256(self):
"""Issue #2264 https://github.com/python-pillow/Pillow/issues/2264"""
# Arrange
im = Image.open("Tests/images/hopper_256x256.ico")
outfile = self.tempfile("temp_saved_hopper_256x256.ico")
# Act
im.save(outfile)
im_saved = Image.open(outfile)
# Assert
self.assertEqual(im_saved.size, (256, 256))
2016-11-30 17:36:04 +03:00
def test_only_save_relevant_sizes(self):
"""Issue #2266 https://github.com/python-pillow/Pillow/issues/2266
Should save in 16x16, 24x24, 32x32, 48x48 sizes
and not in 16x16, 24x24, 32x32, 48x48, 48x48, 48x48, 48x48 sizes
"""
# Arrange
im = Image.open("Tests/images/python.ico") # 16x16, 32x32, 48x48
outfile = self.tempfile("temp_saved_python.ico")
# Act
im.save(outfile)
im_saved = Image.open(outfile)
# Assert
self.assertEqual(
2019-06-13 18:54:11 +03:00
im_saved.info["sizes"], {(16, 16), (24, 24), (32, 32), (48, 48)}
)
2019-05-12 13:44:29 +03:00
def test_unexpected_size(self):
# This image has been manually hexedited to state that it is 16x32
# while the image within is still 16x16
2019-06-13 18:54:11 +03:00
im = self.assert_warning(
UserWarning, Image.open, "Tests/images/hopper_unexpected.ico"
)
2019-05-12 13:44:29 +03:00
self.assertEqual(im.size, (16, 16))
2019-05-29 14:05:45 +03:00
def test_draw_reloaded(self):
im = Image.open(TEST_ICO_FILE)
outfile = self.tempfile("temp_saved_hopper_draw.ico")
draw = ImageDraw.Draw(im)
2019-06-19 09:30:10 +03:00
draw.line((0, 0) + im.size, "#f00")
2019-05-29 14:05:45 +03:00
im.save(outfile)
im = Image.open(outfile)
im.save("Tests/images/hopper_draw.ico")
reloaded = Image.open("Tests/images/hopper_draw.ico")
self.assert_image_equal(im, reloaded)