In this article, we’ll explore how to convert an image to ASCII art using Python. ASCII art is a technique that allows us to create images using a limited set of characters from the ASCII table. It’s been around since the early days of computing, and it’s still popular among programmers and artists alike.
Before we dive into the code, let’s talk about the process of converting an image to ASCII art. The basic idea is to divide the image into small squares, and then replace each square with a character from the ASCII table based on the average brightness of the pixels within that square. This technique allows us to create an image that is composed entirely of characters.
First we understand,
What is ASCII?
ASCII stands for American Standard Code for Information Interchange. It is a character encoding standard that assigns unique numerical codes to 128 different characters, including letters, numbers, and symbols, that are commonly used in the English language.
ASCII was first developed in the 1960s as a way to standardize the representation of characters in computer systems. It has since become widely used in computing, telecommunications, and other fields that involve the transmission and storage of text-based data.
PyWhatKit is a Python library with various helpful features. It is an easy to use library which does not requires you to do some additional setup.
PyWhatKit is a Python library with different accommodating components. It is a simple to utilize library which doesn’t expect you to do some extra arrangements. This module help us to changing any picture over to ASCII craftsmanship utilizing python
This module has lots of other cool features as well.
If you wish to know more about it, you can refer to pywhatkit Module Documentation.
Now that you are familiar with ASCII basics and have acquired basic knowledge of pywhatkit module.
In order to access the Python library, you need to install it into your Python environment
pip install pywhatkit
Time to Code!
Now, we will import pywhatkit package in our python script. Use the following command to do so.
import pywhatkit as kt
Now that we have imported the library using the command
import pywhatkit as kt, let’s proceed.
Let’s display a welcome message. we will make use of
print("Let's convert images to ASCII art!")
Now, let’s capture the image you wish to convert to ASCII art and let’s store it in source_path.
This is the image I am going to use as demo here. The image depicts a Robot. Make sure to give the correct path here.
If the python script could not locate an image with the given name at the mentioned path, it will result into an error.
Next, create a placeholder to store the output. As its an ASCII file, make sure to use the correct extension, i.e.
.text and let’s store it in
source_path = 'robo.png' target_path = 'demo_ascii_art.text'
Finally, let’s call
NOTE: The output file will be generated in the same folder where the python script resides if no alternate path is mentioned.
More python project to explore
- PYTHON ROCK PAPER SCISSORS GAME
- CURRENCY CONVERTER IN PYTHON
- COVID-19 TRACKER APPLICATION USING PYTHON
Let’s have a look at the output file and its comparison with the actual image file.
Let’s have a look at another example. I am making use of my image as demo here. Post successful run, a new text file will get created and will look something like this
With these steps, we have successfully Converted an image to an ASCII art using python. That’s it!
Thank you for reading, I would love to connect with you at LinkedIn.
Do share your valuable suggestions, I appreciate your honest feedback!