Our Objective: To write a python script that turns any image to ASCII Art format
Python is a very fun language. Trying out different python projects as a beginner always makes you more invested in the language. There are so many projects to start with, but I came across a really fun and simple project that you can try in your initial learning days. We are going to make a converter that converts any given image to its corresponding ASCII format. This is a tiny project and hardly takes 5 minutes of your time. But trying and experimenting is always good to be a better learner and eventually a better programmer.
Before heading onwards to the script, make sure to understand the basics of the project. I have explained everything in detail so that you don’t need to hop on to different sources. So let’s quickly dive into our tutorial:
What is ASCII Art?
You must have seen such an image somewhere! Right? This is ASCII Art. So now you can already relate better to what is ASCII Art. It stands for American Standard Code for Information Interchange.
ASCII/ Word/Keyboard/ Text-based art is a form of art that is made out of computer characters. It involves making text images with the combination of 95 printable characters defined by the ASCII Standard. The images produced can be simple emoticons but can even involve some great artwork.
- Make sure to make a new folder to store all the related files of this particular project. This way it’ll become easy to manage and locate.
- pywhatkit module: This module is responsible for converting any image to ASCII art in no time. Just install it like any other module, type in the following command in the terminal:
pip install pywhatkit
- An image which you wish to convert into ASCII art. Store the image within the same folder to ease out the location path.
Code to Convert Image to ASCII Art
- In your editor, set up a new project and then create a file with the name index.py.
- Now import the required module, i.e., pywhatkit. Make sure to install it beforehand using the above mentioned command. I have imported pywhatkit as pw, so in the later part of this program, I’ll just write pw when using pywhatkit.
import pywhatkit as pw
- Our program will now give us a message that shows us that the conversion is being done. The message reads “Converting your given image to ASCII Art”. You can always customise the message or even completely skip it.
print("Converting your given image to ASCII Art")
- Finally now lets call the method “image_to_ascii_art” from the module pywhatkit. It will convert the given image to its ASCII Art form within a few miliseconds. The converted image needs to get stored somewhere. So two parameters are given along with this method.
- The first parameter represents the source path or the path of the image that you want to convert.
- The second parameter depicts the name of the text file where the converted ASCII image gets stored.
Here the name of my source image is ‘Capture.PNG’ and the target text file name is ‘ASCII.txt’. It will automatically create a text file with the target name where the ASCII image will be stored.
- And lastly you can just print a message to show that the task has been completed.
The converted image will always gets stored within the same folder. If you want to change your destination path, you can always set it in the target path like the following:
You must be wondering about the output of our script. So let’s have a look at that as well.
Taking User Input
Here we’ve asked the user to input the path of the image that he/she wants to convert and also the destination path where he wants to save his converted image.
import pywhatkit as pw print("Welcome! Convert any image to ASCII Art!") src_file = input("Enter your saved image path/name here: ") tar_file = input("Enter your output file name/path here: ") print("Converting your given image to ASCII Art") pw.image_to_ascii_art(src_file, tar_file) print("Task Completed")
So that’s how easy it was to turn an image into ASCII art form. A two lines of code is all it takes to accomplish the task. Pywhatkit library has many more fun methods one can try. I have already tried 2-3 methods from this library. One is converting text to Handwriting, and another is Automate WhatsApp using Pywhatkit, which are also really fun and interesting projects to try.
I have also created a video tutorial of the same, so don’t forget to check that out. Make sure to subscribe us on YouTube.
Please do share your valuable suggestions, I highly appreciate your honest feedback!
Do checkout my other blogposts as well:
If you like the content, please give us a like/follow on the following platforms: