Skip to main content

Converts pictures into ASCII art

Project description

ASCII Magic

Python package that converts images into ASCII art for terminals and HTML. Thanks to Colorama it's compatible with the Windows terminal. Code based on ProfOak's Ascii Py.

Basic usage

import ascii_magic
output = ascii_magic.from_image_file('images/moon.jpg')
ascii_magic.to_terminal(output)

Result:

ASCII Magic example

Available functions

from_image_file()

Converts an image file into ASCII art.

ascii_magic.from_image_file(
    path: str,
    columns: int = 120,
    width_ratio: float = 2.5,
    char: str = None,
    back: Back = None,
    mode: Modes = Modes.TERMINAL,
) -> str
  • path => a PIL-compatible file, such as a jpeg or png
  • columns (optional) => the number of characters per row, more columns = wider art
  • width_ratio (optional) => ASCII characters are not squares, so this adjusts the width to height ratio
  • char (optional) => instead of using many different ASCII glyphs, you can use a single one, such as '#'
  • back (optional) => In terminal mode, sets the background color with one of:
    • ascii_magic.Back.BLACK
    • ascii_magic.Back.RED
    • ascii_magic.Back.GREEN
    • ascii_magic.Back.YELLOW
    • ascii_magic.Back.BLUE
    • ascii_magic.Back.MAGENTA
    • ascii_magic.Back.CYAN
    • ascii_magic.Back.WHITE
  • mode (optional) => one of:
    • ascii_magic.Modes.TERMINAL => outputs ASCII with terminal color codes (8 tones)
    • ascii_magic.Modes.ASCII => outputs pure ASCII with no color codes, resulting in a "grayscale" image
    • ascii_magic.Modes.HTML_TERMINAL => outputs HTML simulating terminal colors; wrap it in a <pre>
    • ascii_magic.Modes.HTML => as above, but with full color

Example:

output = ascii_magic.from_image_file(
    'images/lion.jpg',
    columns=200,
    back=ascii_magic.Back.BLUE
)

Result:

ASCII Magic HTML mode example

Example:

output = ascii_magic.from_image_file(
    'images/lion.jpg',
    columns=200,
    width_ratio=2,
    mode=ascii_magic.Modes.HTML
)

Result:

ASCII Magic HTML mode example

Example:

output = ascii_magic.from_image_file(
    'images/lion.jpg',
    columns=200,
    mode=ascii_magic.Modes.ASCII
)

Result:

ASCII Magic HTML mode example

from_url()

As above, but using the URL of an image.

ascii_magic.from_url(
    url: str,
    # ... as above
) -> str
  • url => an URL which will be loaded via urllib (supports redirects)

Example:

img_url = 'https://source.unsplash.com/800x600?nature'
output = ascii_magic.from_url(img_url, columns=100)

from_clipboard()

As above, but with the contents of the clipboard. Raises ValueError if the clipboard doesn't contain an image.

ascii_magic.from_clipboard(
    # ... as above
) -> str

Example:

output = ascii_magic.from_clipboard(columns=100)

from_image()

As above, but using an image loaded with Pillow.

ascii_magic.from_image(
    img: Image,
    # ... as above
) -> str
  • img => PIL image object

Example:

from PIL import Image
with Image.open('images/1.jpg') as img:
    output = ascii_magic.from_image(img, columns=100)

to_terminal()

Initializes Colorama, which is required on Windows for displaying art in terminal mode, and prints the input. It's the same as doing colorama.init() before running print().

ascii_magic.to_terminal(input: str) -> None

quick_test()

Runs ascii_magic with a random Unsplash picture with the default parameters and prints it to the terminal.

ascii_magic.quick_test() -> None

Licence

Copyright (c) 2020 Leandro Barone.

Usage is provided under the MIT License. See LICENSE for the full details.

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

ascii_magic-1.5.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

ascii_magic-1.5.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file ascii_magic-1.5.0.tar.gz.

File metadata

  • Download URL: ascii_magic-1.5.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.4

File hashes

Hashes for ascii_magic-1.5.0.tar.gz
Algorithm Hash digest
SHA256 70e3477587cdbaf26ed6555f4a20d7f77172859441238bb34e07e6a95c9e012d
MD5 9f6847c5f779868a5932b02f664b26eb
BLAKE2b-256 eb391fd936449ae209c4d723067b9dc3a0a676ff15a46311b82c8b3e83235843

See more details on using hashes here.

File details

Details for the file ascii_magic-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: ascii_magic-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.4

File hashes

Hashes for ascii_magic-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5b58e4bf0c638b879021c81d2adae3e6e03658faa38dd9635daeefa2e8c8532f
MD5 1824b42c82ce7278268c2ae5a4fbeb4a
BLAKE2b-256 da87a3b06c007f6d69777decee611a9ee6fa75b92a301fac0a53b178c1f10d5e

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