Displaying an image from a database in Perspective (without Blob modules or WebDev)

I was looking for a simple way to display an image stored in a database inside Perspective, and I accidentally found a very clean solution.
No Blob module, no WebDev, no endpoints — just pure Python inside a component change script.

This approach works with DB column containing raw image bytes (BLOB).

Named Query example

Query name: img_select
Path: MyProject/img_select

sql

SELECT img AS "Content", 'image/png' AS "ContentType"
FROM MyTable
WHERE description = :name

Perspective change script example

python


import base64

result = system.db.runNamedQuery("MyProject", "img_select", {"name": currentValue.value})
content = result.getValueAt(0, "Content") if result.getRowCount() else None

# If no image → send a 1×1 transparent PNG
if not content or len(content) == 0:
    blank_png_base64 = (
        "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8"
        "/w8AAn8B9p2u3wAAAABJRU5ErkJggg=="
    )
    value = "data:image/png;base64," + blank_png_base64
    self.getSibling("Image").props.source = value

else:
    # Convert BLOB → base64
    b64 = base64.b64encode(content).decode("utf-8")
    value = "data:image/png;base64," + b64

    # Send to the Image component
    self.getSibling("Image").props.source = value

Notes

  • No WebDev or file endpoints needed.

  • The Image component accepts base64 directly via props.source.

It would be even more clean if you didn’t import “system” in your script. This is normally an artifact of AI writing code…

That aside, this is effectively the normal way that database image retrieval works in Perspective, and this also works in the Reporting module as well.
The Blob server module has specific use cases that aren’t always needed, but when you need it, you can’t easily AI script around it.

Good point — the import system line isn’t needed in Perspective scripts, so I removed it. And yes, this is the standard way to pull images from a DB in Perspective. Blob Server is only required for the cases where base64 isn’t enough.