Images¶
Image loads and decodes images with the platform's native image loader.
Each platform caches responses and cancels obsolete requests when a source
changes or a component unmounts. Python doesn't download or decode image
bytes.
Sources can be a bundled Asset, an https URL, a
file path, or a data: URI. default_source shows a local image while a
remote one loads (and keeps it if the load fails); blur_radius blurs the
decoded bitmap. Supply layout constraints when an image must reserve space
before its content loads.
pn.Image(
source="https://example.com/photo.jpg",
default_source=pn.asset("images/placeholder.png"),
blur_radius=4,
style=pn.style(width=200, height=120),
)
Images module¶
Images measures, prefetches, and clears the
cache of the same pipeline:
size = await pn.Images.get_size(pn.asset("images/hero.png"))
await pn.Images.prefetch("https://example.com/photo.jpg")
pn.Images.clear_cache()
Image utilities backed by the native image pipeline.
Images exposes the parts of the platform image
loader that don't fit on the Image element:
measuring an image before laying it out, warming the cache, and clearing
it. Every method takes the same source values Image does (a
bundled Asset, a URL, a data: URI, or a file
path) and runs off the main thread natively.
Example
Off device (tests, pn preview) sizes are read from the file headers
of PNG, JPEG, GIF, WebP, and BMP files, and prefetch is a no-op that
reports whether the source is reachable.
Classes:
| Name | Description |
|---|---|
ImageSize |
Logical dimensions of an image, in points. |
Images |
Measure, prefetch, and clear images through the native loader. |
ImageSize
dataclass
¶
Images
¶
Measure, prefetch, and clear images through the native loader.
Raises:
| Type | Description |
|---|---|
NativeModuleError
|
If the native module reports a failure (for instance an unreachable URL or a missing asset). |
Methods:
| Name | Description |
|---|---|
get_size |
Return the logical size of |
prefetch |
Download and cache |
clear_cache |
Drop the in-memory and on-disk image caches. |
get_size
async
staticmethod
¶
Return the logical size of source without displaying it.
Bundled assets report the size of their 1x variant (the
variant's pixel size divided by its scale), so the result is
the size the image would take in layout.
prefetch
async
staticmethod
¶
Download and cache source ahead of time; True on success.
Next steps¶
- Bundle images, fonts, and vectors: Assets guide.