Skip to content

Commit b846e7e

Browse files
authored
Merge pull request #180 from scientificworld/master
feat: support JPEG XL
2 parents 5ddaef8 + 10cd913 commit b846e7e

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Image
6666
- **xcf** - ``image/x-xcf``
6767
- **jpg** - ``image/jpeg``
6868
- **jpx** - ``image/jpx``
69+
- **jxl** - ``image/jxl``
6970
- **png** - ``image/png``
7071
- **apng** - ``image/apng``
7172
- **gif** - ``image/gif``
@@ -161,7 +162,7 @@ Font
161162
- **otf** - ``application/font-sfnt``
162163

163164
Application
164-
^^^^^^^^^^^
165+
^^^^^^^^^^^
165166

166167
- **wasm** - ``application/wasm``
167168

filetype/types/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
image.Xcf(),
1818
image.Jpeg(),
1919
image.Jpx(),
20+
image.Jxl(),
2021
image.Apng(),
2122
image.Png(),
2223
image.Gif(),

filetype/types/image.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,38 @@ def match(self, buf):
4848
)
4949

5050

51+
class Jxl(Type):
52+
"""
53+
Implements the JPEG XL image type matcher.
54+
"""
55+
56+
MIME = "image/jxl"
57+
EXTENSION = "jxl"
58+
59+
def __init__(self):
60+
super(Jxl, self).__init__(mime=Jxl.MIME, extension=Jxl.EXTENSION)
61+
62+
def match(self, buf):
63+
return (
64+
(len(buf) > 1 and
65+
buf[0] == 0xFF and
66+
buf[1] == 0x0A) or
67+
(len(buf) > 11 and
68+
buf[0] == 0x00 and
69+
buf[1] == 0x00 and
70+
buf[2] == 0x00 and
71+
buf[3] == 0x00 and
72+
buf[4] == 0x0C and
73+
buf[5] == 0x4A and
74+
buf[6] == 0x58 and
75+
buf[7] == 0x4C and
76+
buf[8] == 0x20 and
77+
buf[9] == 0x0D and
78+
buf[10] == 0x87 and
79+
buf[11] == 0x0A)
80+
)
81+
82+
5183
class Apng(Type):
5284
"""
5385
Implements the APNG image type matcher.

tests/fixtures/sample.jxl

137 KB
Binary file not shown.

tests/test_types.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ def test_guess_jpx(self):
3232
self.assertEqual(kind.mime, 'image/jpx')
3333
self.assertEqual(kind.extension, 'jpx')
3434

35+
def test_guess_jxl(self):
36+
kind = filetype.guess(FIXTURES + '/sample.jxl')
37+
self.assertTrue(kind is not None)
38+
self.assertEqual(kind.mime, 'image/jxl')
39+
self.assertEqual(kind.extension, 'jxl')
40+
3541
def test_guess_gif(self):
3642
kind = filetype.guess(FIXTURES + '/sample.gif')
3743
self.assertTrue(kind is not None)
@@ -56,7 +62,6 @@ def test_guess_m4a(self):
5662
self.assertEqual(kind.mime, 'audio/mp4')
5763
self.assertEqual(kind.extension, 'm4a')
5864

59-
6065
def test_guess_mp4(self):
6166
kind = filetype.guess(FIXTURES + '/sample.mp4')
6267
self.assertTrue(kind is not None)

0 commit comments

Comments
 (0)