2014-07-07 21:03:50 +04:00
|
|
|
from helper import unittest, PillowTestCase
|
2014-03-26 14:49:39 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
2015-04-12 05:58:46 +03:00
|
|
|
import sys
|
|
|
|
|
2014-03-26 14:49:39 +04:00
|
|
|
# sample icon file
|
2015-05-05 11:15:55 +03:00
|
|
|
TEST_FILE = "Tests/images/pillow.icns"
|
|
|
|
data = open(TEST_FILE, "rb").read()
|
2014-03-26 14:49:39 +04:00
|
|
|
|
2014-03-28 13:09:55 +04:00
|
|
|
enable_jpeg2k = hasattr(Image.core, 'jp2klib_version')
|
|
|
|
|
2014-03-26 14:49:39 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestFileIcns(PillowTestCase):
|
2014-03-26 14:49:39 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_sanity(self):
|
|
|
|
# Loading this icon by default should result in the largest size
|
|
|
|
# (512x512@2x) being loaded
|
2015-05-05 11:15:55 +03:00
|
|
|
im = Image.open(TEST_FILE)
|
2014-06-10 13:10:47 +04:00
|
|
|
im.load()
|
|
|
|
self.assertEqual(im.mode, "RGBA")
|
|
|
|
self.assertEqual(im.size, (1024, 1024))
|
|
|
|
self.assertEqual(im.format, "ICNS")
|
2014-03-28 13:09:55 +04:00
|
|
|
|
2015-04-12 05:58:46 +03:00
|
|
|
@unittest.skipIf(sys.platform != 'darwin',
|
|
|
|
"requires MacOS")
|
|
|
|
def test_save(self):
|
2015-05-05 11:15:55 +03:00
|
|
|
im = Image.open(TEST_FILE)
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2015-05-05 19:38:10 +03:00
|
|
|
temp_file = self.tempfile("temp.icns")
|
|
|
|
im.save(temp_file)
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2015-05-05 19:38:10 +03:00
|
|
|
reread = Image.open(temp_file)
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2015-04-12 05:58:46 +03:00
|
|
|
self.assertEqual(reread.mode, "RGBA")
|
|
|
|
self.assertEqual(reread.size, (1024, 1024))
|
|
|
|
self.assertEqual(reread.format, "ICNS")
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_sizes(self):
|
|
|
|
# Check that we can load all of the sizes, and that the final pixel
|
|
|
|
# dimensions are as expected
|
2015-05-05 11:15:55 +03:00
|
|
|
im = Image.open(TEST_FILE)
|
2014-06-10 13:10:47 +04:00
|
|
|
for w, h, r in im.info['sizes']:
|
|
|
|
wr = w * r
|
|
|
|
hr = h * r
|
2015-05-05 11:15:55 +03:00
|
|
|
im2 = Image.open(TEST_FILE)
|
2014-06-10 13:10:47 +04:00
|
|
|
im2.size = (w, h, r)
|
|
|
|
im2.load()
|
|
|
|
self.assertEqual(im2.mode, 'RGBA')
|
|
|
|
self.assertEqual(im2.size, (wr, hr))
|
2014-03-28 13:09:55 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_older_icon(self):
|
|
|
|
# This icon was made with Icon Composer rather than iconutil; it still
|
|
|
|
# uses PNG rather than JP2, however (since it was made on 10.9).
|
|
|
|
im = Image.open('Tests/images/pillow2.icns')
|
|
|
|
for w, h, r in im.info['sizes']:
|
|
|
|
wr = w * r
|
|
|
|
hr = h * r
|
|
|
|
im2 = Image.open('Tests/images/pillow2.icns')
|
|
|
|
im2.size = (w, h, r)
|
|
|
|
im2.load()
|
|
|
|
self.assertEqual(im2.mode, 'RGBA')
|
|
|
|
self.assertEqual(im2.size, (wr, hr))
|
|
|
|
|
|
|
|
def test_jp2_icon(self):
|
|
|
|
# This icon was made by using Uli Kusterer's oldiconutil to replace
|
|
|
|
# the PNG images with JPEG 2000 ones. The advantage of doing this is
|
|
|
|
# that OS X 10.5 supports JPEG 2000 but not PNG; some commercial
|
|
|
|
# software therefore does just this.
|
|
|
|
|
|
|
|
# (oldiconutil is here: https://github.com/uliwitness/oldiconutil)
|
|
|
|
|
|
|
|
if not enable_jpeg2k:
|
|
|
|
return
|
|
|
|
|
|
|
|
im = Image.open('Tests/images/pillow3.icns')
|
|
|
|
for w, h, r in im.info['sizes']:
|
|
|
|
wr = w * r
|
|
|
|
hr = h * r
|
|
|
|
im2 = Image.open('Tests/images/pillow3.icns')
|
|
|
|
im2.size = (w, h, r)
|
|
|
|
im2.load()
|
|
|
|
self.assertEqual(im2.mode, 'RGBA')
|
|
|
|
self.assertEqual(im2.size, (wr, hr))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|