Skip to content

Python lib to create figures with text characters and ANSI colors from an image file

License

Notifications You must be signed in to change notification settings

wbin01/ansicolorimage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AnsiColorImage

Python lib to create figures with text characters and ANSI colors from an image file

https://github.com/wbin01/ansicolorimage

Minimal example.

img = AnsiColorImage(url_image='python.png')
for line in img.ansi_lines:
    print(line)

Image

Definition

Use help() for details.

AnsiColorImage:
    Parameters:
        url_image
    
    Optional parameters:
        brightness
        chars_map
        contrast
        height
        hide_foreground_character
        show_background_color
        width

    Properties:
        ansi_lines
        brightness
        chars_map
        contrast
        height
        hide_foreground_character
        image_accent_color
        show_background_color
        url_image
        width
    
    Methods:
        update_ascii_lines
        

Easy handling

Each item represents a line, making it easy to enter parallel information. I will also use the accent color to color the text with a vintage look, just like the image.

poem = """
    
The Raven

...
 Then this ebony bird beguiling my sad fancy into smiling,
By the grave and stern decorum of the countenance it wore,
“Though thy crest be shorn and shaven, thou,” I said, “art sure no craven,
Ghastly grim and ancient Raven wandering from the Nightly shore—
Tell me what thy lordly name is on the Night’s Plutonian shore!”
            Quoth the Raven “Nevermore.”
...
But the Raven still beguiling all my fancy into smiling,
Straight I wheeled a cushioned seat in front of bird, and bust and door;
    Then, upon the velvet sinking, I betook myself to linking
    Fancy unto fancy, thinking what this ominous bird of yore—
What this grim, ungainly, ghastly, gaunt, and ominous bird of yore
            Meant in croaking “Nevermore.”
...


(By Edgar Allan Poe)

"""
img = AnsiColorImage(
    url_image='poe.jpg',
    height=25,
    width=50,
    show_background_color=True,
    hide_foreground_character=True)

for text_line, img_line in zip(poem.split('\n'), img.ansi_lines):
    print(
        img_line,
        f'\x1b[38;2;{img.image_accent_color}m{text_line}\x1B[0m')
        # See how to format colors -> https://github.com/termstandard/colors

Image

Custom character map

Choosing the character map helps to achieve conceptual aesthetic results. For example, using 0 and 1 alluding to hacking. Work with a gradient of around 20 characters.

img = AnsiColorImage(
        url_image='bin.png',
        height=20,
        width=80,
        contrast=1.2,
        brightness=0.95,
        chars_map=['0', '0', '0', '0', '0'] + ['1'] * 15)
    for line in img.ansi_lines:
        print(line)

Image

Make an animation

img_frames = os.listdir('frames')  # import os
img_frames.sort()  # ['1.png', '2.png', '3.png', '4.png', '5.png', '6.png']

for image in [
    AnsiColorImage(url_image=os.path.join('frames', x), height=20, width=40)
    for x in img_frames] * 5:

    time.sleep(0.2)     # import time
    os.system('clear')  # import os
    for line in image.ansi_lines:
        print(line)

Image

About

Python lib to create figures with text characters and ANSI colors from an image file

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages