Skip to main content

Convert an ANSI string to an image. Great for adding terminal output into a readme.

Project description

Github top language Codacy grade Repository size Issues License Commit activity Last commit PyPI Downloads PyPI Version

AnsiToImg

Project Icon

Convert an ANSI string to an image. Great for adding terminal output into a readme.

Examples

Here is an example of some code and the images it produces:

Functions accept the following arguments:

  • ansiText - text to process
  • fileName - name of the file to write to
  • theme - a base24 theme. Defaults to atom one dark
import sys
import os
from pathlib import Path
import platform
import ctypes
from catimage.catimage import generateHDColour

THISDIR = str(Path(__file__).resolve().parent)
sys.path.insert(0, os.path.dirname(THISDIR))
from ansitoimg.render import ansiToSVG, ansiToRaster, ansiToSVGRaster, ansiToHTML, ansiToHTMLRaster


if platform.system() == "Windows":
	kernel32 = ctypes.windll.kernel32
	kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)

# Define ANSI text
example = "👋\033[32mHello\033[0m, \033[34mWorld\033[0m🌏\033[31m!\033[0m\n\033[41m👋\033[0m\033[43m🦄\033[0m\033[42m🐘\033[0m\033[3m\033[9m13\033[0m\033[1m3\033[0m\033[4m7\033[0m\033[46m🍄\033[0m\033[44m🎃\033[0m\033[45m🐦\033[0m"
example2 = "hello\nworld\n\033[42m\033[31mwe meet again\033[0m\nABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz😁😂🤣😃😄😅😆😉😊😋😎😍😘🥰😗😙😚☺🙂🤗🤩🤔🤨😐😑😶🙄😏😣😥😮🤐😯😪asdfghjk"
example3 = generateHDColour(THISDIR + "/test.png", 40)

# Print
print(example)
print()
print(example2)
print()
print(example3)
print()

# To SVG
ansiToSVG(example, THISDIR + "/example.svg")
ansiToSVG(example2, THISDIR + "/example2.svg")
ansiToSVG(example3, THISDIR + "/example3.svg")

# To Raster
ansiToRaster(example, THISDIR + "/example.png")
ansiToRaster(example2, THISDIR + "/example2.png")
ansiToRaster(example3, THISDIR + "/example3.png")

# To SVGRaster
ansiToSVGRaster(example, THISDIR + "/svgExample.png")
ansiToSVGRaster(example2, THISDIR + "/svgExample2.png")
ansiToSVGRaster(example3, THISDIR + "/svgExample3.png")

# To HTML
ansiToHTML(example, THISDIR + "/example.html")
ansiToHTML(example2, THISDIR + "/example2.html")
ansiToHTML(example3, THISDIR + "/example3.html")

# To HTMLRaster
ansiToHTMLRaster(example, THISDIR + "/htmlExample.png")
ansiToHTMLRaster(example2, THISDIR + "/htmlExample2.png")
ansiToHTMLRaster(example3, THISDIR + "/htmlExample3.png")

SVG Image

example

example2

example3

Raster Image

example

example2

example3

SVGRaster Image

example

example2

example3

HTML/ HTMLRaster Image

example

example2

example3

Windows Terminal

winterm

Choosing ansiToSVG, ansiToRaster, ansiToSVGRaster, ansiToHTML or ansiToHTMLRaster

ansiToSVG

This is better for the vast majority of cases as the image sizes are smaller for reasonably simple ANSI sequences. The image size scales proportionally with the length of the ANSI sequence. A large number of applications tend to opt for shorter sequences for output making ansiToSVG the better option. ansiToSVG also handles emoji as well as the OS does. For instance, on Windows 10 one can expect full colour emoji. Image sizes can get out of hand for some cases such as catimage output as those tend to be very long ANSI sequences.

ansiToRaster

The image size does not scale to the length of the ANSI sequence but does scale to the number of lines of terminal output. This is ideal for output of complex ANSI sequences that would be huge if ansiToSVG were used. However, emojis are in black and white and show quite poorly on coloured backgrounds.

ansiToSVGRaster

Takes the advantages that ansiToRaster has whilst keeping colour emojis, Yay! This uses pyppeteer to fire up a headless browser which opens the SVG and takes a screenshot.

ansiToHTML

Has the same advantages and disadvantages of ansiToSVG though this is not suitable to be included in a GitHub readme

ansiToHTMLRaster

Has the same advantages and disadvantages of ansiToSVGRaster

Changelog

See the CHANGELOG for more information.

Install With PIP

pip install ansitoimg

Head to https://pypi.org/project/ansitoimg/ for more info

Language information

Built for

This program has been written for Python 3 and has been tested with Python version 3.8.0 https://www.python.org/downloads/release/python-380/.

Install Python on Windows

Chocolatey

choco install python

Download

To install Python, go to https://www.python.org/ and download the latest version.

Install Python on Linux

Apt

sudo apt install python3.8

How to run

With VSCode

  1. Open the .py file in vscode
  2. Ensure a python 3.8 interpreter is selected (Ctrl+Shift+P > Python:Select Interpreter > Python 3.8)
  3. Run by pressing Ctrl+F5 (if you are prompted to install any modules, accept)

From the Terminal

./[file].py

How to update, build and publish

  1. Ensure you have installed the following dependencies Linux
    wget dephell.org/install | python3.8
    wget https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3.8
    
    Windows
    (wget dephell.org/install -UseBasicParsing).Content | python
    (wget https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python
    
  2. Use poetry for the heavy lifting and dephell to generate requirements
    poetry update
    dephell deps convert
    
  3. Build/ Publish
    poetry build
    poetry publish
    
    or
    poetry publish --build
    

Community Files

Licence

MIT License Copyright (c) FredHappyface (See the LICENSE for more information.)

Changelog

See the Changelog for more information.

Code of Conduct

In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project and our community a harassment-free experience for everyone. Please see the Code of Conduct for more information.

Contributing

Contributions are welcome, please see the Contributing Guidelines for more information.

Security

Thank you for improving the security of the project, please see the Security Policy for more information.

Support

Thank you for using this project, I hope it is of use to you. Please be aware that those involved with the project often do so for fun along with other commitments (such as work, family, etc). Please see the Support Policy for more information.

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

ansitoimg-2021.tar.gz (9.2 MB view details)

Uploaded Source

Built Distribution

ansitoimg-2021-py3-none-any.whl (9.2 MB view details)

Uploaded Python 3

File details

Details for the file ansitoimg-2021.tar.gz.

File metadata

  • Download URL: ansitoimg-2021.tar.gz
  • Upload date:
  • Size: 9.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Windows/10

File hashes

Hashes for ansitoimg-2021.tar.gz
Algorithm Hash digest
SHA256 5ca17ad4f2014f50cc3b1776b91db64fed536eb5da2ed7917d25907619451294
MD5 376671aa5601619c72246431f5b98e57
BLAKE2b-256 540664a6dab1a9570a37158435bcdf0103bc3d63a7cd5c19c19cbecac86d0064

See more details on using hashes here.

File details

Details for the file ansitoimg-2021-py3-none-any.whl.

File metadata

  • Download URL: ansitoimg-2021-py3-none-any.whl
  • Upload date:
  • Size: 9.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Windows/10

File hashes

Hashes for ansitoimg-2021-py3-none-any.whl
Algorithm Hash digest
SHA256 4fc6c0f717bf499a97d476c2d8996e0b0c67ce78d2f10b329c6810c9816f98ac
MD5 3cbc386931dbd800e5516f765a65b2cf
BLAKE2b-256 3e6f80d53d526481cf7fa9f52e859658896259cf373c72d60640c61b8c7abd9b

See more details on using hashes here.

Supported by

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