Skip to content

feat: support JPEG XL #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Image
- **xcf** - ``image/x-xcf``
- **jpg** - ``image/jpeg``
- **jpx** - ``image/jpx``
- **jxl** - ``image/jxl``
- **png** - ``image/png``
- **apng** - ``image/apng``
- **gif** - ``image/gif``
Expand Down Expand Up @@ -161,7 +162,7 @@ Font
- **otf** - ``application/font-sfnt``

Application
^^^^^^^^^^^
^^^^^^^^^^^

- **wasm** - ``application/wasm``

Expand Down
1 change: 1 addition & 0 deletions filetype/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
image.Xcf(),
image.Jpeg(),
image.Jpx(),
image.Jxl(),
image.Apng(),
image.Png(),
image.Gif(),
Expand Down
32 changes: 32 additions & 0 deletions filetype/types/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@ def match(self, buf):
)


class Jxl(Type):
"""
Implements the JPEG XL image type matcher.
"""

MIME = "image/jxl"
EXTENSION = "jxl"

def __init__(self):
super(Jxl, self).__init__(mime=Jxl.MIME, extension=Jxl.EXTENSION)

def match(self, buf):
return (
(len(buf) > 1 and
buf[0] == 0xFF and
buf[1] == 0x0A) or
(len(buf) > 11 and
buf[0] == 0x00 and
buf[1] == 0x00 and
buf[2] == 0x00 and
buf[3] == 0x00 and
Copy link
Contributor

@lpetre lpetre Apr 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have one too many 0x00 bytes here and missing a 0x0A later
Fix here: #195

buf[4] == 0x0C and
buf[5] == 0x4A and
buf[6] == 0x58 and
buf[7] == 0x4C and
buf[8] == 0x20 and
buf[9] == 0x0D and
buf[10] == 0x87 and
buf[11] == 0x0A)
)


class Apng(Type):
"""
Implements the APNG image type matcher.
Expand Down
Binary file added tests/fixtures/sample.jxl
Binary file not shown.
7 changes: 6 additions & 1 deletion tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def test_guess_jpx(self):
self.assertEqual(kind.mime, 'image/jpx')
self.assertEqual(kind.extension, 'jpx')

def test_guess_jxl(self):
kind = filetype.guess(FIXTURES + '/sample.jxl')
self.assertTrue(kind is not None)
self.assertEqual(kind.mime, 'image/jxl')
self.assertEqual(kind.extension, 'jxl')

def test_guess_gif(self):
kind = filetype.guess(FIXTURES + '/sample.gif')
self.assertTrue(kind is not None)
Expand All @@ -56,7 +62,6 @@ def test_guess_m4a(self):
self.assertEqual(kind.mime, 'audio/mp4')
self.assertEqual(kind.extension, 'm4a')


def test_guess_mp4(self):
kind = filetype.guess(FIXTURES + '/sample.mp4')
self.assertTrue(kind is not None)
Expand Down