Skip to content

Instantly share code, notes, and snippets.

@eric-czech
Last active October 23, 2024 18:43
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)))
@deblinux123
Copy link

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

@deblinux123
Copy link

how can i fix that error

@SVHawk13
Copy link

SVHawk13 commented Oct 23, 2024

how can i fix that error

You may have gotten your raw image bytes in a way that is unsupported by Pillow:

import io
from pathlib import Path

from PIL import Image

image_path = Path("image.png")
with image_path.open("rb") as fh:
    raw_image: bytes = fh.read()

raw_image_buffer = io.BytesIO(raw_image)
# `.seek(0)` moves the current position in the buffer to the beginning of the file.
# This ensures you read the entire buffer if you call `.read()` on it.
# If you don't do this, `.read()` will return no data because you are at the end of the buffer.
# Assigning to `_` designates that we don't want the value returned by `.seek(0)`.
# This prevents the output from displaying in the REPL.
_ = raw_image_buffer.seek(0)
# This will load the PNG correctly.
image = Image.open(raw_image_buffer)

# Lets set up an incorrect example:
raw_image_2 = image.tobytes()
raw_image_buffer_2 = io.BytesIO(raw_image_2)
_ = raw_image_buffer_2.seek(0)
image2 = Image.open(raw_image_buffer_2)
# PIL.UnidentifiedImageError: cannot identify image file

Why did this error occur, even though we went from bytes -> BytesIO -> PIL.Image.Image like in the first example? It is because the PIL.Image.Image method .tobytes() returns the pixel data of an image, not a fully encoded image. When you try to use Image.open on just pixel data, it can't determine the filetype, because there is no filetype for that data at all, just raw pixels.

Fixing our incorrect example:

raw_image_buffer_3 = io.BytesIO()
# We save the image data to a file-like object (in this case, our BytesIO object).
# `format="PNG"` is required when writing to a BytesIO object here.
# This because there is no file extension for Pillow to infer the filetype automatically.
image.save(raw_image_buffer_3, format="PNG")
_ = raw_image_buffer_3.seek(0)
image3 = Image.open(raw_image_buffer_3)
# Success!

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