Skip to main content

QuranMediaLib

A high-performance media generation library for rendering Quranic Arabic text and translations into customizable, professional-grade images.

Requirements: Python 3.13+


📌 Table of Contents


User Guide

Installation

# Clone the repository
git clone https://github.com/AtTheZenith/quranmedialib.git
cd quranmedialib

# Install with uv (recommended)
uv pip install -e .

Quick Start

from quranmedialib import DatabaseManager, VerseWorkflow, LANDSCAPE_PRESET

# Initialize database manager
db = DatabaseManager()

# Configure layout using a preset (1080p Landscape Default)
layout, text_cfg, word_cfg = LANDSCAPE_PRESET["default"]["1080p"]
workflow = VerseWorkflow(layout, text_cfg, word_cfg)

# Render Surah 1, Ayah 1
translations = ["In the name of Allah,", "the Entirely Merciful, the Especially Merciful."]
pages = list(workflow.get_iterator(surah=1, ayah=1, translations=translations))

# Save first page
pages[0][0].save("output.png")

db.close()

Presets Reference

The library uses a resolution-independent system. All sizing parameters scale linearly from a 1080p baseline.

Aspect Ratio Preset Resolutions Modes
16:9 LANDSCAPE_PRESET 720p, 1080p, 1440p, 2160p default, arabic, translation
9:16 STORY_PRESET 720p, 1080p, 1440p, 2160p default, arabic, translation
1:1 SQUARE_PRESET 720p, 1080p, 1440p, 2160p default, arabic, translation

Built-in Workflows

Workflows are high-level orchestrators that handle data retrieval, layout, and rendering.

SurahWorkflow

Processes an entire surah page by page.

from quranmedialib import SurahWorkflow, LANDSCAPE_PRESET

layout, text, word = LANDSCAPE_PRESET["default"]["1080p"]
workflow = SurahWorkflow(layout, text, word)

for page_num, page_images in enumerate(workflow.get_iterator(surah=112), 1):
    for img, suffix in page_images:
        img.save(f"surah112_p{page_num}_{suffix}.png")

VerseRangeWorkflow

Processes a range of verses with support for parallel rendering.

from quranmedialib import VerseRangeWorkflow, SQUARE_PRESET

layout, text, word = SQUARE_PRESET["default"]["1080p"]
workflow = VerseRangeWorkflow(layout, text, word)

# translations: list[list[str]] -> [verse_index][page_index]
translations = [["Trans V1"], ["Trans V2"]] 
iterator = workflow.get_iterator(surah=1, start_ayah=1, end_ayah=2, translations=translations)

Demo Gallery

Visual examples of generated content:

Preset Image
Landscape (1080p)
Story (1080p)
Square (1080p)

Developer Guide

Architecture

graph TD
    DB[(SQLite Database)] --> WF[Workflow Orchestrator]
    WF --> Layout[Layout Engine: Framer]
    Layout --> Pipe[Rendering Pipeline]
    Pipe --> Mask[Mask-First Rendering]
    Mask --> Comp[Composite/Colorize]
    Comp --> Out[Final Image]
    
    subgraph Config
        L[LayoutConfig] -.-> Layout
        W[WordConfig] -.-> Pipe
        T[TextConfig] -.-> Pipe
    end

Core Thesis

QuranMediaLib is built on the "Boring Code" philosophy: prioritizing linear, obvious logic over clever abstractions to ensure long-term maintainability.

Key Engineering Pillars:

  • Resolution Independence: Layouts are defined relative to a 1080p height and scaled linearly.
  • Memory Efficiency: Use of __slots__ for high-frequency objects and lru_cache for database queries.
  • Performance: Mask-first rendering minimizes expensive RGBA operations.

Core API Reference

DatabaseManager

A thread-safe singleton managing SQLite connections to Quranic databases.

  • get_verse(surah, ayah): Retrieves verse text.
  • get_verses_from_surah(surah): Retrieves all verses in a surah.
  • minimize_caches(): Explicitly clears internal LRU caches.

Configuration

  • LayoutConfig: Controls canvas dimensions, padding, and alignment.
  • WordConfig: Defines Arabic font, size, colors, and spacing.
  • TextConfig: Defines translation font, size, and rich-text formatting.

Resource Management

Custom assets can be loaded via:

  • FontResource.from_path("path/to/font.ttf")
  • DatabaseConfig.from_path("path/to/db.sqlite")

Advanced Workflows

IsolateWordsWorkflow

Used to isolate individual words within their layout context, useful for word-by-word study tools.

from quranmedialib import IsolateWordsWorkflow, LANDSCAPE_PRESET

layout, text, word = LANDSCAPE_PRESET["default"]["1080p"]
workflow = IsolateWordsWorkflow(layout, text, word)

# Isolates each word of the verse
iterator = workflow.get_iterator(
    surah=1, 
    verse_words=["الله", "الرحمن", "الرحيم"], 
    translations=["Allah", "The Merciful", "The Compassionate"]
)

Performance & Parallelism

For bulk rendering, the library provides ParallelRenderer, which distributes tasks across CPU cores.

  • Execution Modes: ExecutionMode.PROCESS (recommended for CPU-heavy tasks) or ExecutionMode.THREAD.
  • Memory Guard: The system monitors aggregate RSS to prevent OOM crashes during large Surah renders.

Development Suite

# Install dev dependencies
uv pip install -e ".[dev]"

# Run all tests
uv run -m pytest -v

# Run benchmarks
uv run -m pytest -v --benchmark

# Lint and Format
uv run -m ruff check .
uv run -m ruff format .

Community & License

We welcome contributions from developers who value engineering rigor and performance.

Release files for quranmedialib 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for quranmedialib 2.0.1
File Size Uploaded
quranmedialib-2.0.1.tar.gz 3.4 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for quranmedialib 2.0.1
File Interpreter ABI Platform
quranmedialib-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 6.8 MB

Release files / quranmedialib-2.0.1.tar.gz

Download URL quranmedialib-2.0.1.tar.gz
Size 3.4 MB
Tags Source
SHA-256 checksum
How to use checksums
7d6fc2453c78e8a17138c1f1e9659887564f2453aeed4f49f5e3c1fc8c19aa31
BLAKE2b-256 checksum
How to use checksums
fdbd61c94987fab7073eea483688a03c597cb12ec2fed249910a0a1da93c158d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.17

Release files / quranmedialib-2.0.1-py3-none-any.whl

Download URL quranmedialib-2.0.1-py3-none-any.whl
Size 3.4 MB
Tags Python 3
SHA-256 checksum
How to use checksums
776035e9968a8e0fae7f2655334daac9f062aceb0f1db18b4df28dbb56b84efc
BLAKE2b-256 checksum
How to use checksums
e83918661df737610e7599dfa2d16aac3331236174d838e2ea878aa795bb2bc8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.17

Release history Release notifications | RSS feed

5.0.0

2 release files

4.2.0

2 release files

4.1.1

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.0.0

2 release files

This release

2.0.1 This release

2 release files

2.0.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page