This commit is contained in:
Hugo 2014-05-10 03:50:21 +00:00
commit e805e9e594
4 changed files with 56 additions and 25 deletions

View File

@ -15,10 +15,11 @@
__version__ = "0.1"
from PIL import Image, ImageFile, _binary
import struct
import os
from PIL import Image, ImageFile
import io
import os
import struct
def _parse_codestream(fp):
"""Parse the JPEG 2000 codestream to extract the size and component
@ -28,7 +29,7 @@ def _parse_codestream(fp):
lsiz = struct.unpack('>H', hdr)[0]
siz = hdr + fp.read(lsiz - 2)
lsiz, rsiz, xsiz, ysiz, xosiz, yosiz, xtsiz, ytsiz, \
xtosiz, ytosiz, csiz \
xtosiz, ytosiz, csiz \
= struct.unpack('>HHIIIIIIIIH', siz[:38])
ssiz = [None]*csiz
xrsiz = [None]*csiz
@ -45,12 +46,13 @@ def _parse_codestream(fp):
elif csiz == 3:
mode = 'RGB'
elif csiz == 4:
mode == 'RGBA'
mode = 'RGBA'
else:
mode = None
return (size, mode)
def _parse_jp2_header(fp):
"""Parse the JP2 header box to extract size, component count and
color space information, returning a PIL (size, mode) tuple."""
@ -90,7 +92,7 @@ def _parse_jp2_header(fp):
if tbox == b'ihdr':
height, width, nc, bpc, c, unkc, ipr \
= struct.unpack('>IIHBBBB', content)
= struct.unpack('>IIHBBBB', content)
size = (width, height)
if unkc:
if nc == 1:
@ -112,13 +114,13 @@ def _parse_jp2_header(fp):
elif nc == 4:
mode = 'RGBA'
break
elif cs == 17: # grayscale
elif cs == 17: # grayscale
if nc == 1:
mode = 'L'
elif nc == 2:
mode = 'LA'
break
elif cs == 18: # sYCC
elif cs == 18: # sYCC
if nc == 3:
mode = 'RGB'
elif nc == 4:
@ -127,6 +129,7 @@ def _parse_jp2_header(fp):
return (size, mode)
##
# Image plugin for JPEG2000 images.
@ -180,10 +183,12 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
ImageFile.ImageFile.load(self)
def _accept(prefix):
return (prefix[:4] == b'\xff\x4f\xff\x51'
or prefix[:12] == b'\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a')
# ------------------------------------------------------------
# Save support

Binary file not shown.

Binary file not shown.

View File

@ -1,7 +1,6 @@
from tester import *
from PIL import Image
from PIL import ImageFile
codecs = dir(Image.core)
@ -15,18 +14,20 @@ ignore('Not enough memory to handle tile data')
test_card = Image.open('Tests/images/test-card.png')
test_card.load()
def roundtrip(im, **options):
out = BytesIO()
im.save(out, "JPEG2000", **options)
bytes = out.tell()
out.seek(0)
im = Image.open(out)
im.bytes = bytes # for testing only
im.bytes = bytes # for testing only
im.load()
return im
# ----------------------------------------------------------------------
def test_sanity():
# Internal version number
assert_match(Image.core.jp2klib_version, '\d+\.\d+\.\d+$')
@ -39,6 +40,7 @@ def test_sanity():
# ----------------------------------------------------------------------
# These two test pre-written JPEG 2000 files that were not written with
# PIL (they were made using Adobe Photoshop)
@ -48,6 +50,7 @@ def test_lossless():
im.save('/tmp/test-card.png')
assert_image_similar(im, test_card, 1.0e-3)
def test_lossy_tiled():
im = Image.open('Tests/images/test-card-lossy-tiled.jp2')
im.load()
@ -55,43 +58,52 @@ def test_lossy_tiled():
# ----------------------------------------------------------------------
def test_lossless_rt():
im = roundtrip(test_card)
assert_image_equal(im, test_card)
def test_lossy_rt():
im = roundtrip(test_card, quality_layers=[20])
assert_image_similar(im, test_card, 2.0)
def test_tiled_rt():
im = roundtrip(test_card, tile_size=(128, 128))
assert_image_equal(im, test_card)
def test_tiled_offset_rt():
im = roundtrip(test_card, tile_size=(128, 128), tile_offset=(0, 0),
offset=(32, 32))
assert_image_equal(im, test_card)
def test_irreversible_rt():
im = roundtrip(test_card, irreversible=True, quality_layers=[20])
assert_image_similar(im, test_card, 2.0)
def test_prog_qual_rt():
im = roundtrip(test_card, quality_layers=[60, 40, 20], progression='LRCP')
assert_image_similar(im, test_card, 2.0)
def test_prog_res_rt():
im = roundtrip(test_card, num_resolutions=8, progression='RLCP')
assert_image_equal(im, test_card)
# ----------------------------------------------------------------------
def test_reduce():
im = Image.open('Tests/images/test-card-lossless.jp2')
im.reduce = 2
im.load()
assert_equal(im.size, (160, 120))
def test_layers():
out = BytesIO()
test_card.save(out, 'JPEG2000', quality_layers=[100, 50, 10],
@ -108,3 +120,17 @@ def test_layers():
im.layers = 3
im.load()
assert_image_similar(im, test_card, 0.4)
def test_rgba():
# Arrange
j2k = Image.open('Tests/images/rgb_trns_ycbc.j2k')
jp2 = Image.open('Tests/images/rgb_trns_ycbc.jp2')
# Act
j2k.load()
jp2.load()
# Assert
assert_equal(j2k.mode, 'RGBA')
assert_equal(jp2.mode, 'RGBA')