Modules#

The following is the API documentation for the Brainfuck Art library. It includes all available functions and classes, automatically extracted from the source code.

brainfuck_art.brainfuck_generation.text_to_bf(text: str) str#

Return Brainfuck code that prints the input text. The text has to be pure ASCII.

brainfuck_art.brainfuck_interpreter.execute_bf(code: str, initial_memory_size: int = 3, max_ops: int = 10000, max_number: int = 256) tuple[str, list[int]]#

A Brainfuck interpreter that executes the given code.

Parameters:
  • code – A string containing Brainfuck code.

  • initial_memory_size – Initial size of the memory tape.

  • max_ops – Maximum number of operations allowed.

Returns:

A tuple containing the output produced by the Brainfuck code and the truncated memory tape.

brainfuck_art.image_processing.image_to_matrix(image: Image | str | Path, text: str | None = None, width: int = 512, height: int = 512, alphabet: str | None = None) tuple[ndarray, ndarray]#

Generates two matrices: one for text and one for the color values of an image.

Parameters:
  • image – The input image, which can be a PIL Image, a file path, or a string path.

  • text – Optional text to encode in the text matrix using Brainfuck.

  • width – The target width for both the image and text matrices.

  • height – The target height for both the image and text matrices.

  • alphabet – Optional custom alphabet for generating the text matrix.

Returns:

A tuple containing the text matrix and the color matrix.

brainfuck_art.color_utils.adjust_color_brightness(rgb: tuple, factor: float) tuple#

Adjust the brightness of an RGB color by converting to CIELAB, modifying the luminance, and converting back to RGB.

Parameters:
  • rgb – The original RGB color as a tuple.

  • factor – Factor to adjust brightness by (greater than 1 to lighten, between 0 and 1 to darken).

Returns:

A new RGB tuple with adjusted brightness.

brainfuck_art.color_utils.get_lighter_text_color(hex_color: str, lighten_factor: float = 1.2) str#

Lighten the background color to use as the text color.

Parameters:
  • hex_color – The hex color string of the background.

  • lighten_factor – Factor to lighten the text color.

Returns:

A hex color string for the adjusted text color.

brainfuck_art.color_utils.hex_to_rgb(hex_color: str) tuple#

Convert hex color string to RGB tuple.

brainfuck_art.color_utils.lab_to_rgb(lab)#

Convert LAB color space to RGB color space.

Parameters:

lab – A tuple representing the LAB color (L, a, b).

Returns:

A tuple representing the corresponding RGB color (R, G, B) on a 0-255 scale.

brainfuck_art.color_utils.luminance(rgb: tuple) float#

Calculate the luminance of an RGB color.

brainfuck_art.color_utils.rgb_to_hex(rgb: tuple) str#

Convert an RGB tuple to a hex color string.

brainfuck_art.color_utils.rgb_to_lab(input_color)#

Convert RGB to CIELAB color space.

Parameters:

input_color – A tuple or list representing an RGB color (R, G, B).

Returns:

A list representing the corresponding LAB color [L, a, b].

Based on @manojpandey/rgb2lab.py gist. Link: https://gist.github.com/manojpandey/f5ece715132c572c80421febebaf66ae

brainfuck_art.output_pdf.save_matrix_to_pdf(text_matrix: np.ndarray, color_matrix: np.ndarray, output_file: str | Path, font_size: int = 10, line_height_ratio: float = 1.0, lighten_factor: float = 1.2) Path#

Generates a PDF file displaying the text matrix with colors from the color matrix, adjusting the text color to be a little bit lighter than the background color.

Parameters:
  • text_matrix – A 2D NumPy array of characters representing the text.

  • color_matrix – A 2D NumPy array of hex color strings.

  • output_file – The file path where the PDF file will be saved.

  • font_size – The font size to use in the PDF output.

  • line_height_ratio – The ratio of line-height to font-size to adjust the aspect ratio (default is 1).

  • lighten_factor – Factor to lighten the text color relative to the background color.

Returns:

The path to the generated PDF file.

brainfuck_art.output_html.save_matrix_to_html(text_matrix: np.ndarray, color_matrix: np.ndarray, output_file: str | Path, font_size: int = 10, line_height_ratio: float = 1.0, lighten_factor: float = 1.2, title: str = 'Hi!') Path#

Generates an HTML file displaying the text matrix with colors from the color matrix, adjusting the text color to be a little bit lighter than the background color.

Parameters:
  • text_matrix – A 2D NumPy array of characters representing the text.

  • color_matrix – A 2D NumPy array of hex color strings.

  • output_file – The file path where the HTML file will be saved.

  • font_size – The font size to use in the HTML output.

  • line_height_ratio – The ratio of line-height to font-size to adjust the aspect ratio (default is 1).

  • lighten_factor – Factor to lighten the text color relative to the background color.

brainfuck_art.output_svg.save_matrix_to_svg(text_matrix: np.ndarray, color_matrix: np.ndarray, output_file: str | Path, font_size: int = 10, line_height_ratio: float = 1.0, lighten_factor: float = 1.2) Path#

Generates an SVG file displaying the text matrix with colors from the color matrix, adjusting the text color to be a little bit lighter than the background color.

Parameters:
  • text_matrix – A 2D NumPy array of characters representing the text.

  • color_matrix – A 2D NumPy array of hex color strings.

  • output_file – The file path where the SVG file will be saved.

  • font_size – The font size to use in the SVG output.

  • line_height_ratio – The ratio of line-height to font-size to adjust the aspect ratio (default is 1).

  • lighten_factor – Factor to lighten the text color relative to the background color.

Returns:

The path to the generated SVG file.