Next case
Ascii Art Generator
Role / Services
Software Development
Credits
David Nwachukwu
Location & year
The United Kingdom ©
2021
In this project, I created a Python program that converts images into art pieces made up of only characters (ASCII) with colors from the image. I wanted to demonstrate my versatile array of skills, so I decided to take on this solo project to showcase my abilities.
Technical Details
To accomplish this, I used the PIL (Python Imaging Library) module, specifically, the Image, ImageDraw, and ImageFont modules. These modules allowed me to manipulate images, draw text, and create fonts, respectively.
The first step was to import the necessary modules and define the output functions. In this project, I created three output functions that would generate different image sizes. Each output function consists of the same basic steps with slight differences in scale factor, character array, and output file name.
Next, I defined the character array, which consists of different characters that can be used to create the ASCII art. I used 69 characters ranging from dark to light. The interval is then calculated based on the number of characters in the array and the range of color values in the image.
After defining the character array, I defined the image file path and loaded the image using the Image module. The image is then resized according to the scale factor, which is a value between 0 and 1. The output size is determined by multiplying the original size with the scale factor.
Next, I loaded the pixel values of the resized image and used the average of the RGB values to calculate the grayscale value of each pixel. I then used the getChar() function to map each grayscale value to its corresponding character in the character array.
Finally, I wrote the characters to a text file and drew the characters on a new image using the ImageDraw module. I used the fill argument to color each character according to its corresponding RGB values.
Learning and Experiences
Working on this project gave me a deeper understanding of image manipulation in Python, including how to load, resize, and manipulate images. I also learned how to convert an image into grayscale and how to use the PIL modules to draw text and create fonts.
I also learned the importance of code organization and the benefits of using functions to modularize code. In this project, I created three output functions with slight differences in parameters instead of duplicating the same code three times. This made my code more readable and easier to maintain.
Conclusion
Overall, this project allowed me to demonstrate my proficiency in Python and the PIL module, specifically in image manipulation and text drawing. I learned valuable skills in modularizing code and code organization that I can apply to future projects. I also had fun creating unique art pieces from images using only characters!