5
$\begingroup$

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?

$\endgroup$

1 Answer 1

7
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ That works flawless! Thanks! $\endgroup$
    – HelloWorld
    Oct 8, 2021 at 2:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .