Asciify is an ASCII art generator built using my own C++ library, Kalam. The main objective of this project was to really see how Kalam is working properly and what it can do.
From way back I wanted to create this project, to convert image into ascii using c++. But I was always failing and finding it hard to do, but I made kalam for simplifying image processing tasks and finally able to give life to this project in so few lines of code because of Kalam.
Asciify takes an image file as input and converts it into an ASCII representation. This is achieved through a series of steps that involve loading the image, converting it to grayscale, adjusting contrast, and finally mapping pixel brightness to ASCII characters.
- Image Loading: The Kalam library makes it easy to load various image formats with minimal code.
- Grayscale Conversion: It uses a weighted sum method to convert RGB values into grayscale values.
- Contrast Adjustment: The brightness of the grayscale image can be adjusted to enhance the final ASCII output.
- ASCII Mapping: Each brightness value is mapped to a corresponding ASCII character to create the final art.
The core functionalities are implemented in the following functions:
- adjustContrast: Adjusts the contrast of a given pixel value.
- mapToAscii: Maps a brightness value to an ASCII character based on predefined characters.
- rgbToGray: Converts an RGB image to grayscale while adjusting the contrast.
- generateAscii: Main function that generates ASCII art from the provided image.
Original Image | ASCII Art |
Original Image | ASCII Art |
To run the Asciify, follow these steps:
- Compile the code: Ensure that you have the
Kalam library
installed and properly linked in your project.
g++ main.cpp -o main -lkalam
- Run the program: Execute the compiled program by providing an image file and optionally a contrast value.
./main <image_file> [contrast]
<image_file>:
Path to the input image. (can take .png or .jpg/jpeg)[contrast]:
Optional contrast value (default is 1.5).
These are some refs I used to make this project:
- https://medium.com/@vinaykattimani2/converting-an-rgb-image-to-a-grayscale-image-cd5b8f4ccf19
- https://www.geeksforgeeks.org/converting-image-ascii-image-python
- https://math.stackexchange.com/questions/906240/algorithms-to-increase-or-decrease-the-contrast-of-an-image
Using my Kalam library for this project has made working with images significantly easier. Its intuitive API allows for quick implementation of complex image processing tasks without the overhead of managing low-level details. This ASCII art generator is just one of many possibilities, and I look forward to exploring more features of Kalam in future projects!
This project is licensed under the MIT License.