Components

Internal classes to generate gif components, included in the documentation for information purposes.

class gifpgn.components._AssetImage(name: str, size: int)[source]

Bases: object

Loads an image from the assets directory or the cache

Parameters:
  • name (str) – filename

  • size (int) – size in pixels to resize the image to

_images: Dict[str, Image] = {}
image() Image[source]

Returns the loaded image

Return Image.Image:

PIL Image object containing the loaded image

class gifpgn.components._Board(size: int, board: Board, reverse: bool = False, square_colors: BoardTheme | None = None, piece_theme: PieceTheme = PieceTheme.ALPHA)[source]

Bases: _Component

Generate an image of a given board position

Parameters:
  • size (int) – Size of the board image in pixels

  • board (chess.Board) – A board state

  • reverse (bool) – Draws the board from the perspective of black if True, defaults to False

  • square_colors (BoardTheme) – Colors of the white and black squares, instance of gifpgn.BoardTheme

  • piece_theme (PieceTheme) – The piece theme to use, instance of gifpgn.PieceTheme

property board: Board
property board_size: int

Size of the board in pixels

draw_arrow(from_sqare: int, to_square: int, color: Literal['red', 'green', 'blue'] = 'green') None[source]

Draws an arrow from one square to another square

Parameters:
  • from_sqare (chess.Square)

  • to_square (chess.Square)

  • color (str) – Arrow color. Options are “red”, “green”, or “blue”. Defaults to “green”

draw_board() None[source]

Draws the full board

draw_nag(nag: Literal['blunder', 'mistake', 'inaccuracy'], square: int) None[source]

Draws a blunder, mistake or inaccuracy NAG at the specified square

Parameters:
  • nag (str) – NAG to draw. Options are “blunder”, “mistake”, or “inaccuracy”

  • square (chess.Square) – The square to draw the NAG

draw_square(square: int) None[source]

Draws a single square

draw_squares(squares: List[int] | None = None) None[source]

Draws the listed squares

get_square_color(square: int) bool[source]

Returns the color of the given square

Parameters:

square (chess.Square)

Return chess.Color:

get_square_image(square: int) Image[source]

Retrieves or creates a square image of the given color

Parameters:

square (chess.Square)

Return Image.Image:

PIL Image object containing an image of the given square color

get_square_position(square: int, center: bool = False) Coord[source]

Calculates the position of either the top left of center of the specified square taking into account whether the board is reversed

Parameters:
  • square (chess.Square)

  • center (bool) – If true the center of the square will be calculated, otherwise top left, defaults to False

Return Coord:

Coordinates of the given square

property square_colors: BoardTheme
class gifpgn.components._Canvas(board: int, analysis_bar: int | None, analysis_graph: int | None, headers: int | None, reverse: bool)[source]

Bases: _Component

Stitches the components together into a final frame

Parameters:
  • board (int) – Board size in pixels

  • analysis_bar (Optional[int]) – Analysis bar width in pixels

  • analysis_graph (Optional[int]) – Graph height in pixels

  • headers (Optional[int]) – Header heights in pixels

  • reverse (bool) – White at the top if True, at the bottom if False

add_bar(bar: Image) None[source]
add_board(board: Image) None[source]
add_graph(graph: Image) None[source]
add_headers(white: Image, black: Image) None[source]
size() Tuple[int, int][source]

Calculates the full canvas size

Return Tuple[int, int]:

x,y tuple

class gifpgn.components._Component[source]

Bases: object

image() Image[source]
class gifpgn.components._EvalBar(size: Tuple[int, int], evalu: Score, max_eval: int, reverse: bool)[source]

Bases: _Component

_draw_eval_bar(evalu: Score) None[source]
_get_bar_position(evalu: Score) int[source]

Returns the y coordinate on the evaluation bar for a given evaluation

Parameters:

evalu (chess.engine.Score)

Return int:

_get_bar_text(evalu: Score) Dict[source]
class gifpgn.components._Graph(game: Game, size: Tuple[int, int], max_eval: int, line_width: int = 1)[source]

Bases: object

Draws the evaluation graph. The full graph is drawn when initialized and stored.

Calls to at_move() return a copy of the graph with a red dot drawn at the specified move.

Parameters:
  • game (chess.pgn.Game) – Game object containing an [%eval ...] annotated PGN

  • size (Tuple[int, int]) – x,y size of the graph

  • max_eval (int) – Limits the y axis to +/- the given number of centipawns

  • line_width (int) – Width of graph line (and x axis line) in pixels, defaults to 1

_draw_graph_background() Image[source]

Iterates through the game in self._game_root and draws a the analysis graph

Return Image.Image:

PIL Image object containing the graph

_get_graph_position(evalu: Score, move: int) Coord[source]

Returns the position of a given evluation and move number on the evaluation graph

Parameters:
  • evalu (chess.engine.Score)

  • move (int)

Return Coord:

Coordinates on the evaluation graph

at_move(move_num: int) Image[source]

Returns a copy of the analysis graph with a red dot drawn at the given move number

Parameters:

move_num (int)

Raises:

MoveOutOfRangeError – Requested move is not valid

Return Image.Image:

class gifpgn.components._Headers(game: Game, captures: List[Piece], size: Tuple[int, int])[source]

Bases: object

_draw_headers(captures: List[Piece]) Dict[bool, Image][source]
image(color: bool) Image[source]

Returns the header for the given chess.Color

Parameters:

color (chess.Color)

Return Image.Image:

class gifpgn.components._Piece(piece: Piece, size: int, theme: PieceTheme = PieceTheme.ALPHA)[source]

Bases: _AssetImage

Extends _AssetImage to convert a chess.Piece to the corresponding filename in the assets directory

Parameters:
  • piece (chess.Piece)

  • size (int) – size in pixels to resize the image to

  • theme (Piecetheme) – Instance of gifpgn.PieceTheme

get_piece_string(piece: Piece) str[source]

Returns the filename of the given piece

Parameters:

piece (chess.Piece)

Return str: