In Blender's Python, I can get the raw bytes that make up an image into a bytearray
, like so:
image = bpy.data.images[0] # Pick any valid image you like.
pixels = bytearray([int(p * 255) for p in image.pixels])
But, instead of raw pixel data, I would like to get a bytearray
containing all the bytes of a PNG-format image. This can be compressed or uncompressed PNG, but I need the bytes to represent a valid PNG format as if for saving to a file, not raw pixel data. Ideally I'd like to get these bytes without actually saving them to disk, and without modifying the user's image if possible (for example they may think the image is a JPG or something). How can I do this? Does Blender have a PNG data format helper function that I could use without disturbing the user's image? If not, is there a code snippet that will convert Blender's internal image representation to PNG format?