# Generated image data disappears

The issue I am having is that at some point, randomly, the pixel data I generate and assign to an image disappears. This usually happens after several large images and pixel data have been created. The images remain, but they are black. If I restart the blend file after saving, they will also turn black. It is almost as if the data I create is freed, same as if I was to use this function:

my_image.buffers_free()


I generate a new image this way:

my_image = bpy.data.images.new(name="new_image", width=1024, height=1024, alpha=True, float_buffer=True)
my_image.pixels = new_pixels


You want to pack the image into the blend "file" right after you set the pixels. I'm not sure if otherwise it's expectable that the data is lost over time (it definitely is if you close and reopen Blender), but I guess it's just kept in volatile memory if you don't pack it.

You can do so by simply calling

my_image.pack(as_png=True)


Note this doesn't mean the blend "file" itself has to be saved, it can in turn be an unsaved instance in memory.

The optional as_png=True is required as Blender otherwise tries to pack a file from disk and store its raw data in the blend, but not having a backing file and thus not knowing the file format, you have to tell it to simply pack it as if it would be an uncompressed PNG file (at least that's how I understood it).

This also makes it possible to mass-dump the generated images via File > External Data > Unpack All Into Files.

To detect if an image has been packed or is in volatile memory, open the UV/Image Editor, select the image in question, and check the text of the "Image" menu to the left of the image selector. If it is postfixed with a star (like Image*), you didn't pack it yet.

• Will this preserve the image as 32bit floating point? – VSB Jun 12 '18 at 14:01
• Good question. So far I've only used integral values and a 32-bit color depth. Since PNG doesn't support 32-bit floats (per channel) ("[63] [...]astronomical image data is usually stored as 32-bit or 64-bit floating-point values, and some raw data is represented as 32-bit integers. Neither format is directly supported by PNG[...]"). I think it's worth a try and see what Blender turns the image data into internally, though the parameter name makes me believe it'll break something. – Ray Jun 12 '18 at 15:39