Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Convert PIL image objects to data uri strings in JSON serialization. #1991

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
Dec 10, 2019
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
23 changes: 14 additions & 9 deletions 23 packages/python/plotly/_plotly_utils/basevalidators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2347,20 +2347,25 @@ def validate_coerce(self, v):
pass
elif self._PIL and isinstance(v, self._PIL.Image.Image):
# Convert PIL image to png data uri string
in_mem_file = io.BytesIO()
v.save(in_mem_file, format="PNG")
in_mem_file.seek(0)
img_bytes = in_mem_file.read()
base64_encoded_result_bytes = base64.b64encode(img_bytes)
base64_encoded_result_str = base64_encoded_result_bytes.decode("ascii")
v = "data:image/png;base64,{base64_encoded_result_str}".format(
base64_encoded_result_str=base64_encoded_result_str
)
v = self.pil_image_to_uri(v)
else:
self.raise_invalid_val(v)

return v

@staticmethod
def pil_image_to_uri(v):
in_mem_file = io.BytesIO()
v.save(in_mem_file, format="PNG")
in_mem_file.seek(0)
img_bytes = in_mem_file.read()
base64_encoded_result_bytes = base64.b64encode(img_bytes)
base64_encoded_result_str = base64_encoded_result_bytes.decode("ascii")
v = "data:image/png;base64,{base64_encoded_result_str}".format(
base64_encoded_result_str=base64_encoded_result_str
)
return v


class CompoundValidator(BaseValidator):
def __init__(self, plotly_name, parent_name, data_class_str, data_docs, **kwargs):
Expand Down
11 changes: 11 additions & 0 deletions 11 packages/python/plotly/_plotly_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re

from _plotly_utils.optional_imports import get_module
from _plotly_utils.basevalidators import ImageUriValidator


PY36_OR_LATER = sys.version_info.major == 3 and sys.version_info.minor >= 6
Expand Down Expand Up @@ -104,6 +105,7 @@ def default(self, obj):
self.encode_as_date,
self.encode_as_list, # because some values have `tolist` do last.
self.encode_as_decimal,
self.encode_as_pil,
)
for encoding_method in encoding_methods:
try:
Expand Down Expand Up @@ -192,6 +194,15 @@ def encode_as_decimal(obj):
else:
raise NotEncodable

@staticmethod
def encode_as_pil(obj):
"""Attempt to convert PIL.Image.Image to base64 data uri"""
pil = get_module("PIL")
if isinstance(obj, pil.Image.Image):
return ImageUriValidator.pil_image_to_uri(obj)
else:
raise NotEncodable


class NotEncodable(Exception):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
from nose.plugins.attrib import attr
from pandas.util.testing import assert_series_equal
import json as _json
import os
import base64

from plotly import optional_imports, utils
from plotly.graph_objs import Scatter, Scatter3d, Figure, Data
from PIL import Image


matplotlylib = optional_imports.get_module("plotly.matplotlylib")
Expand Down Expand Up @@ -274,6 +277,21 @@ def test_datetime_dot_date(self):
j1 = _json.dumps(a, cls=utils.PlotlyJSONEncoder)
assert j1 == '["2014-01-01", "2014-01-02"]'

def test_pil_image_encoding(self):
import _plotly_utils

img_path = os.path.join(
_plotly_utils.__path__[0], "tests", "resources", "1x1-black.png"
)

with open(img_path, "rb") as f:
hex_bytes = base64.b64encode(f.read()).decode("ascii")
expected_uri = "data:image/png;base64," + hex_bytes

img = Image.open(img_path)
j1 = _json.dumps({"source": img}, cls=utils.PlotlyJSONEncoder)
assert j1 == '{"source": "%s"}' % expected_uri


if matplotlylib:

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.