Custom icon previews from internal images

I am trying to create a custom preview collection using utils.previews API something like this:

icons_dict = bpy.utils.previews.new()
icons_dict.load("custom_icon", os.path.join(icons_dir, "icon.png"), 'IMAGE')

I am wondering if it is possible to to load a preview from an internal image instead of file?
I can use the icons_dict.new() API to create an empty preview but I have no idea how to load it with pixels from another image. Any ideas?

Thanks!

Ok, answering my own question:

You can load pixels into an ImagePreview like this

offscreen buffer

off_px = [v / 255 for v in buffer]

custom_icons = preview_collections[“main”]
iprev = custom_icons.new(‘Test_Preview’)
iprev.image_size = (WIDTH,HEIGHT)
iprev.image_pixels_float = off_px

and you can show it anywere like this

global preview_collections
custom_icons = preview_collections[“main”]
layout.template_icon(custom_icons[‘Test_Preview’].icon_id, scale=5.0)

Good job @rpopovici and thanks for sharing the solution here too. :+1: