Skip to main content

Infra-agnostic IIIF Image API 3.0 server built with FastAPI

Project description

SanF

A IIIF Image API 3.0 server implementation built with FastAPI, supporting up to Level 2 compliance. It can be used as a standalone server or embedded as a package within an existing FastAPI application.

Features

  • GET /iiif/{identifier}/info.json
  • GET /iiif/{identifier}/{region}/{size}/{rotation}/{quality}.{format}
  • 303 redirect from GET /iiif/{identifier} to info.json
  • Level 2 parameters:
    • region: full, square, x,y,w,h, pct:x,y,w,h
    • size: max, w,, ,h, pct:n, w,h, !w,h
    • rotation: 0, 90, 180, 270 (and arbitrary non-negative angles)
    • quality: default
    • format: jpg, png
  • CORS enabled (when using create_app, default *)

Installation

pip install sanf

Development Setup

python3.12 -m venv .venv
source .venv/bin/activate
pip install -e .

Running as a Standalone Server

Source images are loaded from ./images by default.

uvicorn sanf.main:create_app --factory --reload

The root directory for the local file connector can be changed via the IIIF_SOURCE_ROOT environment variable.

IIIF_SOURCE_ROOT=/path/to/images uvicorn sanf.main:create_app --factory --reload

Using as a Package

Public API

Symbol Purpose
IIIFServerSettings Settings dataclass
create_app(settings) Creates a standalone FastAPI application
create_iiif_router(settings) Creates an APIRouter for embedding into an existing application
ImageSourceConnector Protocol definition for connectors
LocalFileConnector Connector for local filesystem images
ConnectorError / ImageNotFoundError Connector exceptions

IIIFServerSettings

from sanf import IIIFServerSettings, LocalFileConnector
from pathlib import Path

settings = IIIFServerSettings(
    connector=LocalFileConnector(root=Path("./images")),
    cors_origins=["https://example.com"],   # default: ["*"]
    jpeg_quality=85,                         # default: 85
    max_width=4096,                          # default: None (no limit)
    max_height=4096,                         # default: None (no limit)
)

create_app — Standalone Application

Returns a complete FastAPI instance including CORS middleware.

from sanf import create_app, IIIFServerSettings, LocalFileConnector
from pathlib import Path

settings = IIIFServerSettings(connector=LocalFileConnector(root=Path("./images")))
app = create_app(settings)

create_iiif_router — Embedding into an Existing Application

Returns an APIRouter without CORS. CORS should be managed by the calling application.

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from sanf import create_iiif_router, IIIFServerSettings, LocalFileConnector
from pathlib import Path

settings = IIIFServerSettings(connector=LocalFileConnector(root=Path("./images")))

app = FastAPI()
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["GET"])
app.include_router(create_iiif_router(settings), prefix="/iiif")

Custom Connectors

Any storage backend can be connected by implementing a class that conforms to the ImageSourceConnector protocol.

class MyS3Connector:
    def fetch_image_bytes(self, identifier: str) -> bytes:
        # Return image bytes from S3 or any other backend
        ...
  • Raise ImageNotFoundError when the identifier does not exist
  • Raise ConnectorError for backend failures
  • The identifier is received as a URL-decoded string

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sanf-1.0.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sanf-1.0.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file sanf-1.0.0.tar.gz.

File metadata

  • Download URL: sanf-1.0.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for sanf-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3f4d5044d822d910c93eeae31991ed14a5b7f89e451ee1a0950f825f2977709c
MD5 690a9648ed284aa3ba58c75c607ed69b
BLAKE2b-256 929c9eca3d507fe03327160fc22d342be05248f3484db08e237037c735b11cf5

See more details on using hashes here.

File details

Details for the file sanf-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: sanf-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for sanf-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 57ce37c622c383d0fae2b88e689f3cdabc41d59cd801aa6072210599f9d79531
MD5 602fd074bbc522150038c906717df891
BLAKE2b-256 dd3cc54c84ee68449c8fb2a80b67d17dbe7838c6a6a0439c49df1b0db765ed62

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page