Lets take any jpg image and convert it to ascii art using the python module 'ascii_magic'
SetupThis tutorial assumes you have a valid installation of python and are capable of calling on file locations with a python script.
In a linux terminal, create a new virtual environment for our python package installs. Next, install the ascii_magic and Pillow python module with pip3. Then make a directory for the project folder.
python3 -m venv ascii.art
source ascii.art/bin/activate
pip3 install ascii_magic
pip3 install Pillow
mkdir /ascii.art/project/
WorkflowPlace a jpg/png image in the same directory as the python script that will call on it. In the case below, I've created a blank text file called ascii.art.py and then placed the photo to convert to ascii in the same directory.
Open the script with your favorite text editor
Use the below code in the script, being sure to update the filename to your jpg:
from ascii_magic import AsciiArt
from PIL import ImageEnhance
#create an instance of AsciiArt called my_art and call on the from_image function
#loads a file named ImageForAscii.png
my_art = AsciiArt.from_image('ImageForAscii.png') #Update the filename to your jpg name
my_art.image = ImageEnhance.Brightness(my_art.image).enhance(1.0) #Adjust this float value to increase or decrease the brightness
#print the ascii art to the terminal
my_art.to_terminal()
#-----Optional-----#
#convert the image to a high quality html file called ascii_art.html
my_art.to_html_file('ascii_art.html', columns=200, width_ratio=2)
Save the changes with 'Ctrl+x' and exit the text editor.
Call on the python script which will print the ascii art to terminal
python3 ascii.art.py
If you included the line for creating an html image in the script, then there will be a new file called ascii_art.html in the same directory as the python script and original photo. Open the file in a web browser to view a high-resolution version of the ascii art we created
We can use the Pillow python library to enhance the brightness of the output if the image appears too dark by adjusting the below value from 1.0 to 1.5:
my_art.image = ImageEnhance.Brightness(my_art.image).enhance(1.5) #Adjust this float value to increase or decrease the brightness
Enjoy your custom ascii art thanks to the power of the ascii_magic and Pillow python modules!
Comments
Please log in or sign up to comment.