Made JPEG2K support for files conditional on its inclusion in pillow build. Added a JPEG2K icon test.

This commit is contained in:
Alastair Houghton 2014-03-28 09:09:55 +00:00
parent 0eccdf534d
commit 5aac5b7eff
3 changed files with 33 additions and 1 deletions

View File

@ -15,9 +15,13 @@
# See the README file for information on usage and redistribution.
#
from PIL import Image, ImageFile, PngImagePlugin, Jpeg2KImagePlugin, _binary
from PIL import Image, ImageFile, PngImagePlugin, _binary
import struct, io
enable_jpeg2k = hasattr(Image.core, 'jp2klib_version')
if enable_jpeg2k:
import Jpeg2KImagePlugin
i8 = _binary.i8
HEADERSIZE = 8
@ -101,6 +105,8 @@ def read_png_or_jpeg2000(fobj, start_length, size):
elif sig[:4] == b'\xff\x4f\xff\x51' \
or sig[:4] == b'\x0d\x0a\x87\x0a' \
or sig == b'\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a':
if not enable_jpeg2k:
raise ValueError('Unsupported icon subimage format (rebuild PIL with JPEG 2000 support to fix this)')
# j2k, jpc or j2c
fobj.seek(start)
jp2kstream = fobj.read(length)
@ -109,6 +115,8 @@ def read_png_or_jpeg2000(fobj, start_length, size):
if im.mode != 'RGBA':
im = im.convert('RGBA')
return {"RGBA": im}
else:
raise ValueError('Unsupported icon subimage format')
class IcnsFile:

BIN
Tests/images/pillow3.icns Normal file

Binary file not shown.

View File

@ -6,6 +6,8 @@ from PIL import Image
file = "Images/pillow.icns"
data = open(file, "rb").read()
enable_jpeg2k = hasattr(Image.core, 'jp2klib_version')
def test_sanity():
# Loading this icon by default should result in the largest size
# (512x512@2x) being loaded
@ -40,3 +42,25 @@ def test_older_icon():
im2.load()
assert_equal(im2.mode, 'RGBA')
assert_equal(im2.size, (wr, hr))
def test_jp2_icon():
# 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()
assert_equal(im2.mode, 'RGBA')
assert_equal(im2.size, (wr, hr))