Get the asset preview from Python API

Hello,
I try to work with the asset browser with python.
I can list objects in the asset browser, but I can’t find how to get the preview image generate by Blender.
Is it possible?

Most data blocks seem to have preview:

So probably that has something to do with what you are after.

If you generate a preview it seems to contain an image:

That image seems to be used in asset browser if I make whatever data block into an asset.

Thank you for your reply.
That help me a lot.
If someone need the full code, after reading Blender documentation and tryingdifferent things, I have done this :

            
def toRGBA(pixels, height, width):
    img_data = np.zeros((height, width, 4), dtype=np.uint8)
    for i in range(height * width):
        pixel = pixels[i]
        # Unpack the 32-bit integer into RGBA components
        a = (pixel >> 24) & 0xFF 
        r = (pixel >> 16) & 0xFF 
        g = (pixel >> 8) & 0xFF  
        b = pixel & 0xFF          
        
        # Set the pixel in the NumPy array (row, col, channel)
        row = height-1- (i // width)
        col = i % width
        img_data[row, col] = [r, g, b, a]

    return img_data
        
def createImg(preview, output_path):
        
    preview_data = preview.image_pixels  
    width = preview.image_size[0]
    height = preview.image_size[1]
    
    img_data = toRGBA(preview_data, height, width)
    img = Image.fromarray(img_data, 'RGBA')

    img.save(output_path)
    

You can call it with a preview object and a path for the generated image :
createImg(bpy.data.objects['Cube'].preview, "/tmp/preview_image.png")

2 Likes

Yeah, you can do whatever you want with the image. You can create a new image data block in Blender and copy image pixels to it as well without numpy or anything else:

1 Like