Data-blocks store their own previews, so you can access bpy.types.ImagePreview.icon_id
The icon_id
can be passed to UI elements that take an icon value.
eg:
import bpy
for image in bpy.data.images:
print(image.name, image.preview.icon_id)
This will print the id of already loaded icons.
One area I'd need to look into is how to generate icons from images. The interface does this automatically, but you may want to generate the images on demand (as far as I can see we don't have a direct API call to do this).
Heres a fully working example of using image previews in Blender's interface.
# Run this script and check the object properties.
import bpy
class ImagePreviewPanel(bpy.types.Panel):
bl_label = "Image Icons"
bl_idname = "OBJECT_PT_image_icons"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "object"
def draw(self, context):
layout = self.layout
for image in bpy.data.images:
layout.label(image.name, icon_value=image.preview.icon_id)
if not bpy.data.images:
layout.label("No images!")
def register():
bpy.utils.register_class(ImagePreviewPanel)
def unregister():
bpy.utils.unregister_class(ImagePreviewPanel)
if __name__ == "__main__":
register()