Detect whether mimetype is image/jp2 or image/jpx

This commit is contained in:
Andrew Murray 2019-01-02 15:39:39 +11:00
parent 42e2bed4a6
commit bd7422f934
3 changed files with 15 additions and 4 deletions

BIN
Tests/images/balloon.jpf Normal file

Binary file not shown.

View File

@ -39,6 +39,12 @@ class TestFileJpeg2k(PillowTestCase):
self.assertEqual(im.mode, 'RGB') self.assertEqual(im.mode, 'RGB')
self.assertEqual(im.size, (640, 480)) self.assertEqual(im.size, (640, 480))
self.assertEqual(im.format, 'JPEG2000') self.assertEqual(im.format, 'JPEG2000')
self.assertEqual(im.get_format_mimetype(), 'image/jp2')
def test_jpf(self):
im = Image.open('Tests/images/balloon.jpf')
self.assertEqual(im.format, 'JPEG2000')
self.assertEqual(im.get_format_mimetype(), 'image/jpx')
def test_invalid_file(self): def test_invalid_file(self):
invalid_file = "Tests/images/flower.jpg" invalid_file = "Tests/images/flower.jpg"

View File

@ -57,10 +57,11 @@ def _parse_codestream(fp):
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 (size, mode, mimetype) tuple."""
# Find the JP2 header box # Find the JP2 header box
header = None header = None
mimetype = None
while True: while True:
lbox, tbox = struct.unpack('>I4s', fp.read(8)) lbox, tbox = struct.unpack('>I4s', fp.read(8))
if lbox == 1: if lbox == 1:
@ -75,6 +76,10 @@ def _parse_jp2_header(fp):
if tbox == b'jp2h': if tbox == b'jp2h':
header = fp.read(lbox - hlen) header = fp.read(lbox - hlen)
break break
elif tbox == b'ftyp':
if fp.read(4) == b'jpx ':
mimetype = 'image/jpx'
fp.seek(lbox - hlen - 4, os.SEEK_CUR)
else: else:
fp.seek(lbox - hlen, os.SEEK_CUR) fp.seek(lbox - hlen, os.SEEK_CUR)
@ -145,7 +150,7 @@ def _parse_jp2_header(fp):
if size is None or mode is None: if size is None or mode is None:
raise SyntaxError("Malformed jp2 header") raise SyntaxError("Malformed jp2 header")
return (size, mode) return (size, mode, mimetype)
## ##
# Image plugin for JPEG2000 images. # Image plugin for JPEG2000 images.
@ -165,7 +170,8 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
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) header = _parse_jp2_header(self.fp)
self._size, self.mode, self.custom_mimetype = header
else: else:
raise SyntaxError('not a JPEG 2000 file') raise SyntaxError('not a JPEG 2000 file')
@ -281,4 +287,3 @@ Image.register_extensions(Jpeg2KImageFile.format,
[".jp2", ".j2k", ".jpc", ".jpf", ".jpx", ".j2c"]) [".jp2", ".j2k", ".jpc", ".jpf", ".jpx", ".j2c"])
Image.register_mime(Jpeg2KImageFile.format, 'image/jp2') Image.register_mime(Jpeg2KImageFile.format, 'image/jp2')
Image.register_mime(Jpeg2KImageFile.format, 'image/jpx')