Skip to content
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

Fix: Replace deprecated pkg_resources with packaging #14

Merged
merged 1 commit into from
Feb 1, 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
32 changes: 14 additions & 18 deletions pillow_jxl/JpegXLImagePlugin.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,57 @@
from io import BytesIO

import PIL
from packaging.version import parse
from PIL import Image, ImageFile
from pkg_resources import parse_version

from pillow_jxl import Decoder, Encoder

_VALID_JXL_MODES = {"RGB", "RGBA", "L", "LA"}


def _accept(data):
return data[:2] == b'\xff\x0a' \
or data[:12] == b'\x00\x00\x00\x0c\x4a\x58\x4c\x20\x0d\x0a\x87\x0a' \
return (
data[:2] == b"\xff\x0a"
or data[:12] == b"\x00\x00\x00\x0c\x4a\x58\x4c\x20\x0d\x0a\x87\x0a"
or data[4:7] == b"JXL"
)


class JXLImageFile(ImageFile.ImageFile):

format = "JXL"
format_description = "Jpeg XL image"
__loaded = -1
__frame = 0

def _open(self):

self.fc = self.fp.read()
self._decoder = Decoder()

self._jxlinfo, self._data = self._decoder(self.fc)
self._size = (self._jxlinfo.width, self._jxlinfo.height)
self.rawmode = self._jxlinfo.mode
# NOTE (Isotr0py): PIL 10.1.0 changed the mode to property, use _mode instead
if parse_version(PIL.__version__) >= parse_version("10.1.0"):
if parse(PIL.__version__) >= parse("10.1.0"):
self._mode = self.rawmode
else:
self.mode = self.rawmode

self.tile = []

def seek(self, frame):

self.load()

if self.__frame+1 != frame:
if self.__frame + 1 != frame:
# I believe JPEG XL doesn't support seeking in animations
raise NotImplementedError(
'Seeking more than one frame forward is currently not supported.'
"Seeking more than one frame forward is currently not supported."
)
self.__frame = frame

def load(self):

if self.__loaded != self.__frame:

if self._data is None:
EOFError('no more frames')
EOFError("no more frames")

self.__loaded = self.__frame

Expand All @@ -70,24 +67,23 @@ def tell(self):


def _save(im, fp, filename, save_all=False):

if im.mode not in _VALID_JXL_MODES:
raise NotImplementedError('Only RGB, RGBA, L, LA are supported.')
raise NotImplementedError("Only RGB, RGBA, L, LA are supported.")

info = im.encoderinfo.copy()

# default quality is 1
lossless = info.get('lossless', False)
lossless = info.get("lossless", False)
quality = 0 if lossless else 1
decoding_speed = info.get('decoding_speed', 0)
use_container = info.get('use_container', True)
decoding_speed = info.get("decoding_speed", 0)
use_container = info.get("use_container", True)

enc = Encoder(
mode=im.mode,
lossless=lossless,
quality=quality,
decoding_speed=decoding_speed,
use_container=use_container
use_container=use_container,
)
data = enc(im.tobytes(), im.width, im.height)
fp.write(data)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ classifiers = [
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = [
"packaging",
"Pillow",
]

Expand Down
Loading