mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-05 21:10:11 +03:00
Merge 98e4dfeefb
into 958397ae02
This commit is contained in:
commit
e805e9e594
|
@ -15,20 +15,21 @@
|
||||||
|
|
||||||
__version__ = "0.1"
|
__version__ = "0.1"
|
||||||
|
|
||||||
from PIL import Image, ImageFile, _binary
|
from PIL import Image, ImageFile
|
||||||
import struct
|
|
||||||
import os
|
|
||||||
import io
|
import io
|
||||||
|
import os
|
||||||
|
import struct
|
||||||
|
|
||||||
|
|
||||||
def _parse_codestream(fp):
|
def _parse_codestream(fp):
|
||||||
"""Parse the JPEG 2000 codestream to extract the size and component
|
"""Parse the JPEG 2000 codestream to extract the size and component
|
||||||
count from the SIZ marker segment, returning a PIL (size, mode) tuple."""
|
count from the SIZ marker segment, returning a PIL (size, mode) tuple."""
|
||||||
|
|
||||||
hdr = fp.read(2)
|
hdr = fp.read(2)
|
||||||
lsiz = struct.unpack('>H', hdr)[0]
|
lsiz = struct.unpack('>H', hdr)[0]
|
||||||
siz = hdr + fp.read(lsiz - 2)
|
siz = hdr + fp.read(lsiz - 2)
|
||||||
lsiz, rsiz, xsiz, ysiz, xosiz, yosiz, xtsiz, ytsiz, \
|
lsiz, rsiz, xsiz, ysiz, xosiz, yosiz, xtsiz, ytsiz, \
|
||||||
xtosiz, ytosiz, csiz \
|
xtosiz, ytosiz, csiz \
|
||||||
= struct.unpack('>HHIIIIIIIIH', siz[:38])
|
= struct.unpack('>HHIIIIIIIIH', siz[:38])
|
||||||
ssiz = [None]*csiz
|
ssiz = [None]*csiz
|
||||||
xrsiz = [None]*csiz
|
xrsiz = [None]*csiz
|
||||||
|
@ -45,16 +46,17 @@ def _parse_codestream(fp):
|
||||||
elif csiz == 3:
|
elif csiz == 3:
|
||||||
mode = 'RGB'
|
mode = 'RGB'
|
||||||
elif csiz == 4:
|
elif csiz == 4:
|
||||||
mode == 'RGBA'
|
mode = 'RGBA'
|
||||||
else:
|
else:
|
||||||
mode = None
|
mode = None
|
||||||
|
|
||||||
return (size, mode)
|
return (size, mode)
|
||||||
|
|
||||||
|
|
||||||
def _parse_jp2_header(fp):
|
def _parse_jp2_header(fp):
|
||||||
"""Parse the JP2 header box to extract size, component count and
|
"""Parse the JP2 header box to extract size, component count and
|
||||||
color space information, returning a PIL (size, mode) tuple."""
|
color space information, returning a PIL (size, mode) tuple."""
|
||||||
|
|
||||||
# Find the JP2 header box
|
# Find the JP2 header box
|
||||||
header = None
|
header = None
|
||||||
while True:
|
while True:
|
||||||
|
@ -76,7 +78,7 @@ def _parse_jp2_header(fp):
|
||||||
|
|
||||||
size = None
|
size = None
|
||||||
mode = None
|
mode = None
|
||||||
|
|
||||||
hio = io.BytesIO(header)
|
hio = io.BytesIO(header)
|
||||||
while True:
|
while True:
|
||||||
lbox, tbox = struct.unpack('>I4s', hio.read(8))
|
lbox, tbox = struct.unpack('>I4s', hio.read(8))
|
||||||
|
@ -90,7 +92,7 @@ def _parse_jp2_header(fp):
|
||||||
|
|
||||||
if tbox == b'ihdr':
|
if tbox == b'ihdr':
|
||||||
height, width, nc, bpc, c, unkc, ipr \
|
height, width, nc, bpc, c, unkc, ipr \
|
||||||
= struct.unpack('>IIHBBBB', content)
|
= struct.unpack('>IIHBBBB', content)
|
||||||
size = (width, height)
|
size = (width, height)
|
||||||
if unkc:
|
if unkc:
|
||||||
if nc == 1:
|
if nc == 1:
|
||||||
|
@ -112,13 +114,13 @@ def _parse_jp2_header(fp):
|
||||||
elif nc == 4:
|
elif nc == 4:
|
||||||
mode = 'RGBA'
|
mode = 'RGBA'
|
||||||
break
|
break
|
||||||
elif cs == 17: # grayscale
|
elif cs == 17: # grayscale
|
||||||
if nc == 1:
|
if nc == 1:
|
||||||
mode = 'L'
|
mode = 'L'
|
||||||
elif nc == 2:
|
elif nc == 2:
|
||||||
mode = 'LA'
|
mode = 'LA'
|
||||||
break
|
break
|
||||||
elif cs == 18: # sYCC
|
elif cs == 18: # sYCC
|
||||||
if nc == 3:
|
if nc == 3:
|
||||||
mode = 'RGB'
|
mode = 'RGB'
|
||||||
elif nc == 4:
|
elif nc == 4:
|
||||||
|
@ -127,6 +129,7 @@ def _parse_jp2_header(fp):
|
||||||
|
|
||||||
return (size, mode)
|
return (size, mode)
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Image plugin for JPEG2000 images.
|
# Image plugin for JPEG2000 images.
|
||||||
|
|
||||||
|
@ -141,16 +144,16 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
|
||||||
self.size, self.mode = _parse_codestream(self.fp)
|
self.size, self.mode = _parse_codestream(self.fp)
|
||||||
else:
|
else:
|
||||||
sig = sig + self.fp.read(8)
|
sig = sig + self.fp.read(8)
|
||||||
|
|
||||||
if sig == b'\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a':
|
if sig == b'\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a':
|
||||||
self.codec = "jp2"
|
self.codec = "jp2"
|
||||||
self.size, self.mode = _parse_jp2_header(self.fp)
|
self.size, self.mode = _parse_jp2_header(self.fp)
|
||||||
else:
|
else:
|
||||||
raise SyntaxError('not a JPEG 2000 file')
|
raise SyntaxError('not a JPEG 2000 file')
|
||||||
|
|
||||||
if self.size is None or self.mode is None:
|
if self.size is None or self.mode is None:
|
||||||
raise SyntaxError('unable to determine size/mode')
|
raise SyntaxError('unable to determine size/mode')
|
||||||
|
|
||||||
self.reduce = 0
|
self.reduce = 0
|
||||||
self.layers = 0
|
self.layers = 0
|
||||||
|
|
||||||
|
@ -177,13 +180,15 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
|
||||||
t = self.tile[0]
|
t = self.tile[0]
|
||||||
t3 = (t[3][0], self.reduce, self.layers, t[3][3])
|
t3 = (t[3][0], self.reduce, self.layers, t[3][3])
|
||||||
self.tile = [(t[0], (0, 0) + self.size, t[2], t3)]
|
self.tile = [(t[0], (0, 0) + self.size, t[2], t3)]
|
||||||
|
|
||||||
ImageFile.ImageFile.load(self)
|
ImageFile.ImageFile.load(self)
|
||||||
|
|
||||||
|
|
||||||
def _accept(prefix):
|
def _accept(prefix):
|
||||||
return (prefix[:4] == b'\xff\x4f\xff\x51'
|
return (prefix[:4] == b'\xff\x4f\xff\x51'
|
||||||
or prefix[:12] == b'\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a')
|
or prefix[:12] == b'\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a')
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------
|
# ------------------------------------------------------------
|
||||||
# Save support
|
# Save support
|
||||||
|
|
||||||
|
@ -214,7 +219,7 @@ def _save(im, fp, filename):
|
||||||
fd = fp.fileno()
|
fd = fp.fileno()
|
||||||
except:
|
except:
|
||||||
fd = -1
|
fd = -1
|
||||||
|
|
||||||
im.encoderconfig = (
|
im.encoderconfig = (
|
||||||
offset,
|
offset,
|
||||||
tile_offset,
|
tile_offset,
|
||||||
|
@ -229,9 +234,9 @@ def _save(im, fp, filename):
|
||||||
cinema_mode,
|
cinema_mode,
|
||||||
fd
|
fd
|
||||||
)
|
)
|
||||||
|
|
||||||
ImageFile._save(im, fp, [('jpeg2k', (0, 0)+im.size, 0, kind)])
|
ImageFile._save(im, fp, [('jpeg2k', (0, 0)+im.size, 0, kind)])
|
||||||
|
|
||||||
# ------------------------------------------------------------
|
# ------------------------------------------------------------
|
||||||
# Registry stuff
|
# Registry stuff
|
||||||
|
|
||||||
|
|
BIN
Tests/images/rgb_trns_ycbc.j2k
Normal file
BIN
Tests/images/rgb_trns_ycbc.j2k
Normal file
Binary file not shown.
BIN
Tests/images/rgb_trns_ycbc.jp2
Normal file
BIN
Tests/images/rgb_trns_ycbc.jp2
Normal file
Binary file not shown.
|
@ -1,7 +1,6 @@
|
||||||
from tester import *
|
from tester import *
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from PIL import ImageFile
|
|
||||||
|
|
||||||
codecs = dir(Image.core)
|
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 = Image.open('Tests/images/test-card.png')
|
||||||
test_card.load()
|
test_card.load()
|
||||||
|
|
||||||
|
|
||||||
def roundtrip(im, **options):
|
def roundtrip(im, **options):
|
||||||
out = BytesIO()
|
out = BytesIO()
|
||||||
im.save(out, "JPEG2000", **options)
|
im.save(out, "JPEG2000", **options)
|
||||||
bytes = out.tell()
|
bytes = out.tell()
|
||||||
out.seek(0)
|
out.seek(0)
|
||||||
im = Image.open(out)
|
im = Image.open(out)
|
||||||
im.bytes = bytes # for testing only
|
im.bytes = bytes # for testing only
|
||||||
im.load()
|
im.load()
|
||||||
return im
|
return im
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
def test_sanity():
|
def test_sanity():
|
||||||
# Internal version number
|
# Internal version number
|
||||||
assert_match(Image.core.jp2klib_version, '\d+\.\d+\.\d+$')
|
assert_match(Image.core.jp2klib_version, '\d+\.\d+\.\d+$')
|
||||||
|
@ -36,9 +37,10 @@ def test_sanity():
|
||||||
assert_equal(im.mode, 'RGB')
|
assert_equal(im.mode, 'RGB')
|
||||||
assert_equal(im.size, (640, 480))
|
assert_equal(im.size, (640, 480))
|
||||||
assert_equal(im.format, 'JPEG2000')
|
assert_equal(im.format, 'JPEG2000')
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
# These two test pre-written JPEG 2000 files that were not written with
|
# These two test pre-written JPEG 2000 files that were not written with
|
||||||
# PIL (they were made using Adobe Photoshop)
|
# PIL (they were made using Adobe Photoshop)
|
||||||
|
|
||||||
|
@ -48,6 +50,7 @@ def test_lossless():
|
||||||
im.save('/tmp/test-card.png')
|
im.save('/tmp/test-card.png')
|
||||||
assert_image_similar(im, test_card, 1.0e-3)
|
assert_image_similar(im, test_card, 1.0e-3)
|
||||||
|
|
||||||
|
|
||||||
def test_lossy_tiled():
|
def test_lossy_tiled():
|
||||||
im = Image.open('Tests/images/test-card-lossy-tiled.jp2')
|
im = Image.open('Tests/images/test-card-lossy-tiled.jp2')
|
||||||
im.load()
|
im.load()
|
||||||
|
@ -55,49 +58,58 @@ def test_lossy_tiled():
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
def test_lossless_rt():
|
def test_lossless_rt():
|
||||||
im = roundtrip(test_card)
|
im = roundtrip(test_card)
|
||||||
assert_image_equal(im, test_card)
|
assert_image_equal(im, test_card)
|
||||||
|
|
||||||
|
|
||||||
def test_lossy_rt():
|
def test_lossy_rt():
|
||||||
im = roundtrip(test_card, quality_layers=[20])
|
im = roundtrip(test_card, quality_layers=[20])
|
||||||
assert_image_similar(im, test_card, 2.0)
|
assert_image_similar(im, test_card, 2.0)
|
||||||
|
|
||||||
|
|
||||||
def test_tiled_rt():
|
def test_tiled_rt():
|
||||||
im = roundtrip(test_card, tile_size=(128, 128))
|
im = roundtrip(test_card, tile_size=(128, 128))
|
||||||
assert_image_equal(im, test_card)
|
assert_image_equal(im, test_card)
|
||||||
|
|
||||||
|
|
||||||
def test_tiled_offset_rt():
|
def test_tiled_offset_rt():
|
||||||
im = roundtrip(test_card, tile_size=(128, 128), tile_offset=(0, 0),
|
im = roundtrip(test_card, tile_size=(128, 128), tile_offset=(0, 0),
|
||||||
offset=(32, 32))
|
offset=(32, 32))
|
||||||
assert_image_equal(im, test_card)
|
assert_image_equal(im, test_card)
|
||||||
|
|
||||||
|
|
||||||
def test_irreversible_rt():
|
def test_irreversible_rt():
|
||||||
im = roundtrip(test_card, irreversible=True, quality_layers=[20])
|
im = roundtrip(test_card, irreversible=True, quality_layers=[20])
|
||||||
assert_image_similar(im, test_card, 2.0)
|
assert_image_similar(im, test_card, 2.0)
|
||||||
|
|
||||||
|
|
||||||
def test_prog_qual_rt():
|
def test_prog_qual_rt():
|
||||||
im = roundtrip(test_card, quality_layers=[60, 40, 20], progression='LRCP')
|
im = roundtrip(test_card, quality_layers=[60, 40, 20], progression='LRCP')
|
||||||
assert_image_similar(im, test_card, 2.0)
|
assert_image_similar(im, test_card, 2.0)
|
||||||
|
|
||||||
|
|
||||||
def test_prog_res_rt():
|
def test_prog_res_rt():
|
||||||
im = roundtrip(test_card, num_resolutions=8, progression='RLCP')
|
im = roundtrip(test_card, num_resolutions=8, progression='RLCP')
|
||||||
assert_image_equal(im, test_card)
|
assert_image_equal(im, test_card)
|
||||||
|
|
||||||
# ----------------------------------------------------------------------
|
# ----------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
def test_reduce():
|
def test_reduce():
|
||||||
im = Image.open('Tests/images/test-card-lossless.jp2')
|
im = Image.open('Tests/images/test-card-lossless.jp2')
|
||||||
im.reduce = 2
|
im.reduce = 2
|
||||||
im.load()
|
im.load()
|
||||||
assert_equal(im.size, (160, 120))
|
assert_equal(im.size, (160, 120))
|
||||||
|
|
||||||
|
|
||||||
def test_layers():
|
def test_layers():
|
||||||
out = BytesIO()
|
out = BytesIO()
|
||||||
test_card.save(out, 'JPEG2000', quality_layers=[100, 50, 10],
|
test_card.save(out, 'JPEG2000', quality_layers=[100, 50, 10],
|
||||||
progression='LRCP')
|
progression='LRCP')
|
||||||
out.seek(0)
|
out.seek(0)
|
||||||
|
|
||||||
im = Image.open(out)
|
im = Image.open(out)
|
||||||
im.layers = 1
|
im.layers = 1
|
||||||
im.load()
|
im.load()
|
||||||
|
@ -108,3 +120,17 @@ def test_layers():
|
||||||
im.layers = 3
|
im.layers = 3
|
||||||
im.load()
|
im.load()
|
||||||
assert_image_similar(im, test_card, 0.4)
|
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')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user