Skip to content

Instantly share code, notes, and snippets.

@eric-czech
Last active May 29, 2024 09:17
Show Gist options
  • Save eric-czech/fea266e546efac0e704d99837a52b35f to your computer and use it in GitHub Desktop.
Save eric-czech/fea266e546efac0e704d99837a52b35f to your computer and use it in GitHub Desktop.
Convert png image bytes to numpy array
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)))
@FARIDDD123
Copy link

i get this error
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7e34b56d20c0>

@FARIDDD123
Copy link

how can i fix that error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment