In this article, we will learn how to Convert an Image to ASCII art using Python. We will see the implementation in Python.
Check out the Python series . Like it and share it, if you find it useful!
First we understand,
What is ASCII?
ASCII, abbreviated from American Standard Code for Information Interchange.
It is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices.
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.
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!