5

I have a base64 encoded PNG image, and I want to load this into an image. I could write a simple PNG decoder and read every pixel, but this would be very inefficient as this would be done on a per-pixel-basis through Python.

Is there a way to feed bpy.types.Image with the entire raw string of a PNG that doesn't come from disk?

HelloWorld
  • 181
  • 4

1 Answers1

7

This seems to work.

def image_from_data(img_name, data):
   # Create image, width and height are dummy values
   img = bpy.data.images.new(img_name, 8, 8)

Set packed file data

img.pack(data=data, data_len=len(data))

Switch to file source so it uses the packed file

img.source = 'FILE'

return img

You can also change the packed data afterwards with another .pack() call; use .reload() in that case to refresh after the change.

scurest
  • 10,349
  • 13
  • 31