Last active
May 29, 2024 09:17
-
-
Save eric-czech/fea266e546efac0e704d99837a52b35f to your computer and use it in GitHub Desktop.
Convert png image bytes to numpy array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from io import BytesIO | |
from PIL import Image | |
def png_bytes_to_numpy(png): | |
"""Convert png bytes to numpy array | |
Example: | |
>>> fig = go.Figure(go.Scatter(x=[1], y=[1])) | |
>>> plt.imshow(png_bytes_to_numpy(fig.to_image('png'))) | |
""" | |
return np.array(Image.open(BytesIO(png))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i get this error
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7e34b56d20c0>