Skip to content

Commit

Permalink
support jpeg encode
Browse files Browse the repository at this point in the history
  • Loading branch information
Isotr0py committed Feb 3, 2024
1 parent c8baffa commit 92259f7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/target
Cargo.lock

# for test
*.ipynb
*.jxl
*.jpeg
Expand Down
7 changes: 6 additions & 1 deletion pillow_jxl/JpegXLImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ def _save(im, fp, filename, save_all=False):
decoding_speed=decoding_speed,
use_container=use_container,
)
data = enc(im.tobytes(), im.width, im.height)
# FIXME (Isotr0py): im.filename maybe None if parse stream
if im.format == "JPEG" and im.filename:
with open(im.filename, "rb") as f:
data = enc(f.read(), im.width, im.height, jpeg_encode=True)
else:
data = enc(im.tobytes(), im.width, im.height, jpeg_encode=False)
fp.write(data)


Expand Down
14 changes: 12 additions & 2 deletions pillow_jxl/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ class Encoder:
lossless: bool = True,
quality: float = 0.0): ...

def __call__(self, data: bytes, width: int, height: int) -> bytes: ...
def __call__(self, data: bytes, width: int, height: int, jpeg_encode: bool) -> bytes: ...
'''
Encode a jpeg-xl image.
Args:
data(`bytes`): raw image bytes
Return:
`bytes`: The encoded jpeg-xl image.
'''


class Decoder:
Expand All @@ -30,14 +39,15 @@ class Decoder:

def __init__(self, parallel: bool = True): ...

def __call__(self, data: bytes) -> (ImageInfo, bytes): ...
def __call__(self, data: bytes) -> (bool, ImageInfo, bytes): ...
'''
Decode a jpeg-xl image.
Args:
data(`bytes`): jpeg-xl image
Return:
`bool`: If the jpeg is reconstructed
`ImageInfo`: The metadata of decoded image
`bytes`: The decoded image.
'''
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ impl Encoder {
}
}

#[pyo3(signature = (data, width, height))]
fn __call__<'a>(&'a self, _py: Python<'a>, data: &[u8], width: u32, height: u32) -> &PyBytes {
#[pyo3(signature = (data, width, height, jpeg_encode))]
fn __call__<'a>(&'a self, _py: Python<'a>, data: &[u8], width: u32, height: u32, jpeg_encode: bool) -> &PyBytes {
let parallel_runner: ThreadsRunner;
let mut encoder = match self.parallel {
true => {
Expand All @@ -71,7 +71,10 @@ impl Encoder {
encoder.quality = self.quality;
encoder.use_container = self.use_container;
encoder.decoding_speed = self.decoding_speed;
let buffer: EncoderResult<u8> = encoder.encode(&data, width, height).unwrap();
let buffer: EncoderResult<u8> = match jpeg_encode {
true => encoder.encode_jpeg(&data).unwrap(),
false => encoder.encode(&data, width, height).unwrap(),
};
PyBytes::new(_py, &buffer.data)
}

Expand Down

0 comments on commit 92259f7

Please sign in to comment.