|
1 | 1 | import plotly.graph_objs as go |
2 | 2 | from _plotly_utils.basevalidators import ColorscaleValidator |
3 | 3 | from ._core import apply_default_cascade |
4 | | -from io import BytesIO |
5 | | -import base64 |
6 | 4 | from .imshow_utils import rescale_intensity, _integer_ranges, _integer_types |
7 | 5 | import pandas as pd |
8 | | -from .png import Writer, from_array |
9 | 6 | import numpy as np |
| 7 | +from plotly.utils import image_array_to_data_uri |
10 | 8 |
|
11 | 9 | try: |
12 | 10 | import xarray |
13 | 11 |
|
14 | 12 | xarray_imported = True |
15 | 13 | except ImportError: |
16 | 14 | xarray_imported = False |
17 | | -try: |
18 | | - from PIL import Image |
19 | | - |
20 | | - pil_imported = True |
21 | | -except ImportError: |
22 | | - pil_imported = False |
23 | 15 |
|
24 | 16 | _float_types = [] |
25 | 17 |
|
26 | 18 |
|
27 | | -def _array_to_b64str(img, backend="pil", compression=4, ext="png"): |
28 | | - """Converts a numpy array of uint8 into a base64 png string. |
29 | | -
|
30 | | - Parameters |
31 | | - ---------- |
32 | | - img: ndarray of uint8 |
33 | | - array image |
34 | | - backend: str |
35 | | - 'auto', 'pil' or 'pypng'. If 'auto', Pillow is used if installed, |
36 | | - otherwise pypng. |
37 | | - compression: int, between 0 and 9 |
38 | | - compression level to be passed to the backend |
39 | | - ext: str, 'png' or 'jpg' |
40 | | - compression format used to generate b64 string |
41 | | - """ |
42 | | - # PIL and pypng error messages are quite obscure so we catch invalid compression values |
43 | | - if compression < 0 or compression > 9: |
44 | | - raise ValueError("compression level must be between 0 and 9.") |
45 | | - alpha = False |
46 | | - if img.ndim == 2: |
47 | | - mode = "L" |
48 | | - elif img.ndim == 3 and img.shape[-1] == 3: |
49 | | - mode = "RGB" |
50 | | - elif img.ndim == 3 and img.shape[-1] == 4: |
51 | | - mode = "RGBA" |
52 | | - alpha = True |
53 | | - else: |
54 | | - raise ValueError("Invalid image shape") |
55 | | - if backend == "auto": |
56 | | - backend = "pil" if pil_imported else "pypng" |
57 | | - if ext != "png" and backend != "pil": |
58 | | - raise ValueError("jpg binary strings are only available with PIL backend") |
59 | | - |
60 | | - if backend == "pypng": |
61 | | - ndim = img.ndim |
62 | | - sh = img.shape |
63 | | - if ndim == 3: |
64 | | - img = img.reshape((sh[0], sh[1] * sh[2])) |
65 | | - w = Writer( |
66 | | - sh[1], sh[0], greyscale=(ndim == 2), alpha=alpha, compression=compression |
67 | | - ) |
68 | | - img_png = from_array(img, mode=mode) |
69 | | - prefix = "data:image/png;base64," |
70 | | - with BytesIO() as stream: |
71 | | - w.write(stream, img_png.rows) |
72 | | - base64_string = prefix + base64.b64encode(stream.getvalue()).decode("utf-8") |
73 | | - else: # pil |
74 | | - if not pil_imported: |
75 | | - raise ImportError( |
76 | | - "pillow needs to be installed to use `backend='pil'. Please" |
77 | | - "install pillow or use `backend='pypng'." |
78 | | - ) |
79 | | - pil_img = Image.fromarray(img) |
80 | | - if ext == "jpg" or ext == "jpeg": |
81 | | - prefix = "data:image/jpeg;base64," |
82 | | - ext = "jpeg" |
83 | | - else: |
84 | | - prefix = "data:image/png;base64," |
85 | | - ext = "png" |
86 | | - with BytesIO() as stream: |
87 | | - pil_img.save(stream, format=ext, compress_level=compression) |
88 | | - base64_string = prefix + base64.b64encode(stream.getvalue()).decode("utf-8") |
89 | | - return base64_string |
90 | | - |
91 | | - |
92 | 19 | def _vectorize_zvalue(z, mode="max"): |
93 | 20 | alpha = 255 if mode == "max" else 0 |
94 | 21 | if z is None: |
@@ -422,7 +349,7 @@ def imshow( |
422 | 349 | for ch in range(img.shape[-1]) |
423 | 350 | ] |
424 | 351 | ) |
425 | | - img_str = _array_to_b64str( |
| 352 | + img_str = image_array_to_data_uri( |
426 | 353 | img_rescaled, |
427 | 354 | backend=binary_backend, |
428 | 355 | compression=binary_compression_level, |
|
0 commit comments