Skip to content

Add webp support to px.imshow #4797

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix duplicate kwargs error + add test
Signed-off-by: maximsmol <1472826+maximsmol@users.noreply.github.com>
  • Loading branch information
maximsmol committed Oct 11, 2024
commit ba98e7e260b1a70e6211dd82c0704a54e346013c
11 changes: 8 additions & 3 deletions packages/python/plotly/_plotly_utils/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def image_array_to_data_uri(
backend_kwargs = {}

if backend == "pypng":
backend_kwargs.setdefault("compression", compression)

ndim = img.ndim
sh = img.shape
if ndim == 3:
Expand All @@ -62,7 +64,6 @@ def image_array_to_data_uri(
sh[0],
greyscale=(ndim == 2),
alpha=alpha,
compression=compression,
**backend_kwargs,
)
img_png = from_array(img, mode=mode)
Expand All @@ -71,6 +72,12 @@ def image_array_to_data_uri(
w.write(stream, img_png.rows)
base64_string = prefix + base64.b64encode(stream.getvalue()).decode("utf-8")
else: # pil
if ext == "png":
backend_kwargs.setdefault("compress_level", compression)

if ext == "webp":
backend_kwargs.setdefault("lossless", True)

if not pil_imported:
raise ImportError(
"pillow needs to be installed to use `backend='pil'. Please"
Expand All @@ -90,8 +97,6 @@ def image_array_to_data_uri(
pil_img.save(
stream,
format=ext,
compress_level=compression,
lossless=True,
**backend_kwargs,
)
base64_string = prefix + base64.b64encode(stream.getvalue()).decode("utf-8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,15 @@ def test_imshow_backend(backend):
assert np.all(decoded_img == img_rgb)


@pytest.mark.parametrize("lossless", [True, False])
def test_imshow_backend_kwargs(lossless):
fig = px.imshow(img_rgb, binary_backend_kwargs={"lossless": lossless})
decoded_img = decode_image_string(fig.data[0].source)

if lossless:
assert np.all(decoded_img == img_rgb)


@pytest.mark.parametrize("level", [0, 3, 6, 9])
def test_imshow_compression(level):
_, grid_img = np.mgrid[0:10, 0:100]
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy