Core API

Overview

Aspose.Imaging FOSS for Python’s declared public surface is five names, all importable directly from aspose_imaging_foss: the functions detect_format(), probe(), and probe_async(), the result type ImageInfo, and the ImageFormat enum. This page covers how they fit together as a contract, beyond the per-feature tour in Features and Functionalities, and introduces the internal reader classes that implement header parsing underneath them.


The public contract

  • detect_format() / probe() / probe_async() — module-level functions, not methods on a class. Each call is independent; there is nothing to configure or dispose.
  • ImageInfo — the result type. Its five properties (format, width, height, bit_depth, frame_count) are read-only.
  • ImageFormat — an IntEnum with one member per recognized format, plus UNKNOWN.
from aspose_imaging_foss import ImageFormat, ImageInfo, detect_format, probe

info = probe("photo.jpg")
format: ImageFormat = info.format   # always set
width: int | None = info.width      # set only when the format's header carries it

Choosing the right function

FunctionUse when
probe(source)You want the full ImageInfo — dimensions, bit depth, frame count
probe_async(source)Same as probe(), but inside an asyncio application
detect_format(source)You only need to branch on ImageFormat — skip header parsing beyond the first 132 bytes

All three accept a file path, a bytes buffer, or a binary stream; probe_async() also accepts a stream exposing an asynchronous read(size) method.


Batch-probing a directory

Because probe() never raises, a directory scan can call it in a tight loop with no per-file try/except:

import os
from aspose_imaging_foss import ImageFormat, probe

for name in os.listdir("incoming"):
    info = probe(os.path.join("incoming", name))

    if info.format == ImageFormat.UNKNOWN:
        print(f"{name}: not a recognized image format, skipping")
        continue

    print(f"{name}: {info.format} {info.width}x{info.height}")

Any file whose header doesn’t match one of the 14 recognized formats resolves to ImageFormat.UNKNOWN rather than raising — the loop above never needs an except clause.


The never-raises contract

This isn’t incidental behavior — it’s a deliberate part of the API contract. If a recognized format’s header is truncated or malformed partway through parsing, probe() still returns an ImageInfo with format set to the format it did recognize; it does not propagate a parse exception to the caller. Only format is guaranteed — treat every other property as optional regardless of which format was detected.


Internal reader classes

The functions above are implemented on top of a set of internal reader classes in aspose_imaging_foss/_reader.py. These are not re-exported from the package root (exported_via_package_init is not set for any of them in the project’s API surface), so they are internal API — may change between releases. They’re documented here because they explain how header parsing actually works, not as a stable integration point.

  • ProbeIO (internal API — may change between releases) — runs the format parsers with either inline I/O or asyncio-aware I/O, selected by its asynchronous flag. probe() uses it in inline mode; probe_async() uses it in asyncio mode.
  • HeaderReader (internal API — may change between releases) — reads forward from a buffered byte prefix and then continues reading from the original stream if needed, so a parser can consume more of the header than was initially buffered without losing its place in the stream.
  • OffsetReader (internal API — may change between releases) — reads container metadata at an absolute file offset, which formats with a directory-style layout (TIFF’s IFD chain, DjVu’s page index) need for random-access extraction.
  • AsyncReadable (internal API — may change between releases) — a Protocol describing the asynchronous read(size) shape probe_async() accepts from a caller-supplied stream, such as asyncio.StreamReader.

Common Issues

IssueCauseFix
Calling probe() when detect_format() would doUnnecessary header parsing when only the format is neededUse detect_format() if you never read width/height/bit_depth/frame_count
UNKNOWN format for a file you expect to be supportedHeader doesn’t match any of the 14 recognized signatures, or the file is a different format entirelyConfirm the file against the supported formats list
Importing a reader class directlyProbeIO, HeaderReader, OffsetReader, and AsyncReadable are internal and not re-exported from the package rootUse detect_format(), probe(), or probe_async() instead — they are the stable public entry points

FAQ

Is probe() thread-safe?

probe() holds no mutable state between calls — each call is independent, making concurrent calls from multiple threads safe.

Do I need to close anything after probing?

No. If you pass a stream you opened yourself, you remain responsible for closing it — probe()/probe_async() do not close it for you.

Why are there internal reader classes if the public surface is just five names?

They implement the shared header-parsing machinery (buffered reads, offset-based reads, asyncio-aware I/O) that the format-specific parsers for all 14 supported formats build on. They’re an implementation detail, not a second API to integrate against.


API Reference Summary

Function / TypeDescription
detect_format(source)Return only the ImageFormat for a path, buffer, or stream
probe(source)Probe a path, buffer, or stream, returning an ImageInfo
probe_async(source)Asynchronous counterpart to probe()
ImageInfo.format / .width / .height / .bit_depth / .frame_countRead-only result properties
ImageFormatIntEnum: UNKNOWN, PNG, JPEG, GIF, BMP, WEBP, ICO, TIFF, PSD, EMF, WMF, DICOM, DJVU, AVIF, HEIC

See Also