How to Work with the Aspose.Imaging FOSS Core API in Python
View as Markdown

How to Work with the Aspose.Imaging FOSS Core API in Python

Aspose.Imaging FOSS for Python’s public surface is five names: the functions detect_format(), probe(), and probe_async(), the result type ImageInfo, and the ImageFormat enum. This guide walks through installing the package and using them together to detect a format and read header metadata from a file, a buffer, and a stream.

Step-by-Step Guide

Step 1: Install the Package

git clone https://github.com/aspose-imaging-foss/Aspose.Imaging-FOSS-for-Python.git
cd Aspose.Imaging-FOSS-for-Python
pip install -e .

The package targets Python 3.10 or later and has zero dependencies beyond the Python standard library.


Step 2: Import the Functions and Types

A single import statement exposes everything you need:

from aspose_imaging_foss import ImageFormat, ImageInfo, detect_format, probe

Step 3: Probe a File by Path

probe() reads just enough of the file header to populate an ImageInfo result — it never decodes pixel data:

info = probe("photo.jpg")
print(f"{info.format}: {info.width}x{info.height}, {info.bit_depth}-bit")

Step 4: Probe a Buffer or Stream

When the image is already in memory or came from a stream (an upload, a network response), pass it directly instead of writing to a temporary file first:

with open("photo.jpg", "rb") as f:
    data = f.read()
info_from_bytes = probe(data)

with open("photo.jpg", "rb") as stream:
    info_from_stream = probe(stream)

probe() works with both seekable and non-seekable streams.


Step 5: Use detect_format() When You Only Need the Format

If you only need to branch on the format and don’t need dimensions, bit depth, or frame count, detect_format() skips the rest of the header parse:

format = detect_format(data)
if format == ImageFormat.DICOM:
    pass  # route to DICOM-specific handling

Step 6: Read the Nullable ImageInfo Fields Safely

width, height, bit_depth, and frame_count are nullable — they’re populated only when the detected format’s header actually carries that value. format is the one field always set once the header is recognized:

info = probe("scan.dcm")
if info.width is not None and info.height is not None:
    print(f"{info.format}: {info.width}x{info.height}")
else:
    print(f"{info.format}: dimensions not available in this header")

Step 7: Probe Inside an asyncio Application

probe_async() mirrors probe() for asyncio applications, avoiding blocking the event loop:

import asyncio
from aspose_imaging_foss import probe_async

async def main():
    info = await probe_async("drawing.emf")
    print(info.width, info.height)

asyncio.run(main())

Common Issues and Fixes

Format shows as ImageFormat.UNKNOWN. The file’s header doesn’t match any of the 14 recognized formats, or its signature appears beyond the first 132 bytes detect_format() reads. Confirm the file is genuinely one of the supported formats — probe()/detect_format() never raise for an unrecognized header, they report UNKNOWN instead.

width/height are None even though format is set. Not every format’s header carries every field, and a truncated file may cut off before a field’s bytes. Check format first, then treat the rest as optional.

Calling probe() when detect_format() would do. probe() parses the full header; detect_format() only reads enough to identify the format. Use detect_format() if you never read width/height/bit_depth/frame_count.

Expecting decoded pixel data. probe() never decodes, renders, or converts image content — it only reads header metadata. Decoding and pixel-level operations require the commercial Aspose.Imaging for Python SDK.

Frequently Asked Questions

What happens if I probe a truncated or corrupted file?

probe() and probe_async() never raise on malformed or truncated input. They return a partial ImageInfo — format is set whenever the header’s format marker was recognized, even if the rest of the file is cut short.

Is probe() thread-safe?

Yes. probe() holds no mutable state between calls, so concurrent calls from multiple threads are safe.

Is there an asynchronous version of probe()?

Yes. probe_async() mirrors probe() for asyncio applications, accepting a path, buffer, or a stream that exposes an asynchronous read(size) method such as asyncio.StreamReader.

Do I need to close anything?

No. probe()/probe_async() hold no resources of their own. If you pass a stream you opened yourself, you remain responsible for closing it.

See Also