Create Ascii art with Python

Hello all, in today’s quick tutorial I will show you a small Python script that converts image files into beautiful ASCII art.

This is our test image:

Let’s jump to the code:

First we need to import our dependencies:

import sys
from PIL import…

Hello all, in today’s quick tutorial I will show you a small Python script that converts image files into beautiful ASCII art.

This is our test image:

enter image description here
Let’s jump to the code:

First we need to import our dependencies:

import sys
from PIL import Image
from termcolor import colored
import colorama

First we need to read our image:

colorama.init()
try:
    image_path = sys.argv[1].strip('-')
except:
    image_path = input('test1.jpg')

then we will be converting it into grayscale

class AsciiArt:
    def __init__(self, img_path):
        self.path = image_path
        self.img = Image.open(self.path)

    def image(self):

after that, we resize the image:

        width, height = self.img.size
        aspect_ratio = height/width
        new_width = 120
        new_height = aspect_ratio * new_width * 0.55
        img = self.img.resize((new_width, int(new_height)))

new size of image, and we convert image to greyscale format

        img = img.convert('L')
        pixels = img.getdata()

replace each pixel with a character from array

        chars = ["B", "S", "#", "&", "@", "$", "%", "*", "!", ":", "."]
        new_pixels = [chars[pixel//25] for pixel in pixels]
        new_pixels = ''.join(new_pixels)

split string of chars into multiple strings of length equal to new width and create a list

        new_pixels_count = len(new_pixels)
        ascii_image = [new_pixels[index:index + new_width]
                  for index in range(0, new_pixels_count, new_width)]
        ascii_image = "\n".join(ascii_image)
        print(ascii_image)

write to a text file.

      file = "ascii_image.txt"
      with open(file, "w") as f:
            f.write(ascii_image)
            print(colored(f"saved art image to file as {file}", "yellow"))

Last step:

if __name__ == "__main__":
    AsciiArt(image_path).image()

This is our final result –

enter image description here

Thank you all.


Print Share Comment Cite Upload Translate
APA
Stokry | Sciencx (2024-03-29T10:36:10+00:00) » Create Ascii art with Python. Retrieved from https://www.scien.cx/2021/05/04/create-ascii-art-with-python/.
MLA
" » Create Ascii art with Python." Stokry | Sciencx - Tuesday May 4, 2021, https://www.scien.cx/2021/05/04/create-ascii-art-with-python/
HARVARD
Stokry | Sciencx Tuesday May 4, 2021 » Create Ascii art with Python., viewed 2024-03-29T10:36:10+00:00,<https://www.scien.cx/2021/05/04/create-ascii-art-with-python/>
VANCOUVER
Stokry | Sciencx - » Create Ascii art with Python. [Internet]. [Accessed 2024-03-29T10:36:10+00:00]. Available from: https://www.scien.cx/2021/05/04/create-ascii-art-with-python/
CHICAGO
" » Create Ascii art with Python." Stokry | Sciencx - Accessed 2024-03-29T10:36:10+00:00. https://www.scien.cx/2021/05/04/create-ascii-art-with-python/
IEEE
" » Create Ascii art with Python." Stokry | Sciencx [Online]. Available: https://www.scien.cx/2021/05/04/create-ascii-art-with-python/. [Accessed: 2024-03-29T10:36:10+00:00]
rf:citation
» Create Ascii art with Python | Stokry | Sciencx | https://www.scien.cx/2021/05/04/create-ascii-art-with-python/ | 2024-03-29T10:36:10+00:00
https://github.com/addpipe/simple-recorderjs-demo