Python 3 Script to Convert Image Files to ASCII Text Art Codes File Using Pillow Library Full Project For Beginners

Python 3 Script to Convert Image Files to ASCII Text Art Codes File Using Pillow Library Full Project For Beginners

 

Welcome folks today in this blog post we will be converting images files to ascii art codes using pillow library in python. All the full source code of the application is given below.

 

 

 

Get Started

 

 

 

In order to get started you need to install the following libraries using pip command as shown below

 

pip install pillow

 

After installing this library you need to make an app.py file and copy paste the following code

 

app.py

 

 

import sys
from PIL import Image


# pass the image as command line argument
image_path = sys.argv[1]
img = Image.open(image_path)

# resize the image
width, height = img.size
aspect_ratio = height/width
new_width = 120
new_height = aspect_ratio * new_width * 0.55
img = img.resize((new_width, int(new_height)))
# new size of image
# print(img.size)

# convert image to greyscale format
# https://stackoverflow.com/questions/52307290/what-is-the-difference-between-images-in-p-and-l-mode-in-pil
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.
with open("ascii_image.txt", "w") as f:
    f.write(ascii_image)

 

See also  Python 3 WxPython wx.miniframe Popup Child Window GUI Desktop App Full Project For Beginners

 

And now you can execute this python script app.py by typing the below command

 

python app.py ##pathofimagefile##.jpg

 

And now in order to execute this script you need to provide additional command line argument where you will provide the path of the image file for which you need to convert to ascii art codes

 

 

 

 

 

Leave a Reply