Skip to main content

Generate beautiful chess board images from FEN notation

Project description

Chessboard Image

PyPI version Python 3.7+ License: MIT

A pure Python library for generating beautiful chess board images from FEN notation. No external dependencies except Pillow.

Features

Pure Python - No browser dependencies, just PIL/Pillow
🎨 Multiple Themes - Built-in professional piece sets
🎯 FEN Support - Generate boards from any valid FEN notation
📱 Flexible Output - Save as file, get bytes, or PIL Image object
🔧 Player Perspective - View from White or Black's perspective
📍 Coordinates - Optional file/rank labels

Installation

pip install chessboard-image

Quick Start

import chessboard_image as cbi

# Generate starting position
start_fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
cbi.generate_image(start_fen, "board.png", size=400)

Starting Position

Basic Usage

import chessboard_image as cbi

# Basic generation
cbi.generate_image(fen, "board.png", size=400)

# With coordinates
cbi.generate_image(fen, "board.png", show_coordinates=True)

# Black's perspective
cbi.generate_image(fen, "board.png", player_pov="black")

# Different theme
cbi.generate_image(fen, "board.png", theme_name="alpha")

# Get as PIL Image or bytes
pil_image = cbi.generate_pil(fen, size=500)
image_bytes = cbi.generate_bytes(fen)
White Perspective Black Perspective
White POV Black POV
Without Coordinates With Coordinates
No Coordinates With Coordinates

Themes

Built-in themes: alpha, wikipedia, uscf, wisteria, sakura, maestro

Wikipedia Alpha USCF
Wikipedia Alpha USCF
Wisteria Sakura Maestro
Wisteria Sakura Maestro
# List available themes
themes = cbi.list_themes()

# Use different theme
cbi.generate_image(fen, "board.png", theme_name="alpha")

CLI Usage

# Basic generation
chessboard-image generate "fen_string" -o board.png

# With options
chessboard-image generate "fen_string" -o board.png -s 600 -t alpha -p black -c

# Options:
# -s, --size: Board size in pixels
# -t, --theme: Theme name  
# -p, --player-pov: white or black perspective
# -c, --coordinates: Show coordinates

Examples

Famous Positions

import chessboard_image as cbi

# Scholar's Mate
scholars_mate = "r1bqkb1r/pppp1Qpp/2n2n2/4p3/2B1P3/8/PPPP1PPP/RNB1K1NR b KQkq - 0 1"
cbi.generate_image(scholars_mate, "scholars_mate.png", size=500)

# Endgame
endgame = "8/1p1b4/p7/3ppk2/6p1/2P4p/PP3B1K/5B2 b - - 0 1"
cbi.generate_image(endgame, "endgame.png", player_pov="black", show_coordinates=True)

# Sicilian Defense
sicilian = "rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2"
cbi.generate_image(sicilian, "sicilian.png", theme_name="alpha")
Scholar's Mate Endgame (Black POV) Sicilian Defense
Scholar's Mate Endgame Sicilian

Batch Processing

positions = [
    ("start", "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"),
    ("endgame", "8/1B6/8/8/8/8/1K3Qqr/7k w KQkq - 0 1"),
]

for name, fen in positions:
    cbi.generate_image(fen, f"{name}.png", size=400, show_coordinates=True)

API Reference

Core Functions

generate_image(fen, output_path=None, size=400, theme_name="wikipedia", 
              player_pov="white", show_coordinates=False)
generate_bytes(fen, size=400, theme_name="wikipedia", 
              player_pov="white", show_coordinates=False)  
generate_pil(fen, size=400, theme_name="wikipedia",
            player_pov="white", show_coordinates=False)
list_themes()

Parameters

  • fen (str): FEN notation string
  • size (int): Board size in pixels (default: 400)
  • theme_name (str): Theme name (default: "wikipedia")
  • player_pov (str): "white" or "black" perspective (default: "white")
  • show_coordinates (bool): Show file/rank labels (default: False)

Custom Themes

Create a JSON file with base64-encoded pieces:

{
  "my_theme": {
    "pieces": {
      "wK": ["data:image/png;base64,..."], "wQ": ["data:image/png;base64,..."],
      "wR": ["data:image/png;base64,..."], "wB": ["data:image/png;base64,..."], 
      "wN": ["data:image/png;base64,..."], "wP": ["data:image/png;base64,..."],
      "bK": ["data:image/png;base64,..."], "bQ": ["data:image/png;base64,..."],
      "bR": ["data:image/png;base64,..."], "bB": ["data:image/png;base64,..."],
      "bN": ["data:image/png;base64,..."], "bP": ["data:image/png;base64,..."]
    },
    "board": ["#F0D9B5", "#B58863"]
  }
}

Use with: cbi.generate_image(fen, "board.png", theme_file="my_themes.json", theme_name="my_theme")

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Setup

git clone https://github.com/anandjoshi91/chessboard-image.git
cd chessboard-image
pip install -e .
pip install -r requirements-dev.txt

Running Tests

python -m pytest tests/

Requirements

  • Python 3.7+
  • Pillow (PIL)

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

1.1.5

  • Updated USCF theme with improved piece designs

1.1.4

  • Fixed transparency handling for grayscale piece images (LA mode)
  • Now supports RGBA, LA, PA and any image mode with alpha channel
  • Fixes issue where pieces with LA mode showed black backgrounds
  • Added maestro theme (6 built-in themes total)

1.1.3

  • Initial release
  • Support for FEN notation
  • Built-in 5 themes [alpha, wikipedia, uscf, wisteria, sakura]
  • Generate image from pov of black or white player
  • Add optional file and rank coordinates

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

chessboard_image-1.1.5.tar.gz (165.8 kB view details)

Uploaded Source

Built Distribution

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

chessboard_image-1.1.5-py3-none-any.whl (158.2 kB view details)

Uploaded Python 3

File details

Details for the file chessboard_image-1.1.5.tar.gz.

File metadata

  • Download URL: chessboard_image-1.1.5.tar.gz
  • Upload date:
  • Size: 165.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for chessboard_image-1.1.5.tar.gz
Algorithm Hash digest
SHA256 a3334937a3e8a7313a535cd61207b70dcb89320b46b5f1ac1a2efd9f0f9732b5
MD5 b46d51e10036edfd136854fe26d4f4e5
BLAKE2b-256 ef2cd8b3de38a0df3f5114731ea9844b74b2577be601399888cb71134a1c503d

See more details on using hashes here.

File details

Details for the file chessboard_image-1.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for chessboard_image-1.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 03cfa4e3253597fd4479d60f2a26041507cbd601d12d3403a2275ac812464d82
MD5 4a3f042a0beb62f7b215ab4a82c4cf1e
BLAKE2b-256 aa3d07c8c5e47548b9ba24c50446bdfae1da2b399748a32962f67cd521ada97f

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