Image to ASCII - A Python Projects for Beginners Practice
The image to ASCII character is a filtering process to convert an image into an ASCII character representation or to convert an image data into a combination of ASCII characters. "Image to ASCII" is a coding-based technique where the image data is codded into a combination of symbols characters. This mini-project can be considered as "Beginners python projects" or as "cool python projects" for beginners in python.
Steps to solve "Image to ASCII - A Python Projects for Beginners Practice"
1. Select the symbol list to use for the ASCII character printing.
2. Is the input image three channels color, then use RGB list of the threshold.
Or, the image is two channels color, then use a GRAYSCALE list of the threshold.
3. Read the image.
4. Resize the image for easy printing and proper calculation.
5. Find the threshold values of an image by converting the image into binary values.
6. Replace the image values with the RGB list of threshold values or GRAYSCALE list of threshold values.
7. Finally, print the symbol image array as output. The symbols will replace the values of the image's rows and columns according to the threshold values of symbols. The generated symbols output will be shown in the command shell.
Python code for "Image to ASCII - A Python Projects for Beginners Practice"
import cv2
import numpy as np
list_of_symbols = ["#", "-", "*", ".", "+"]
list_of_RGB_threshold = [0, 50, 100, 150, 200]
list_of_GRAYSCALE_threshold=[0, 50, 100]
def print_out_ascii(Generated_ascii_image):
for image_row in Generated_ascii_image:
for image_threshold_values in image_row:
print(list_of_symbols[int(image_threshold_values) % len(list_of_symbols)], end="")
print()
def image_to_ascii_symbol(image):
height, width = image.shape
new_width = int(width / 20)
new_height = int(height / 40)
resized_image = cv2.resize(image, (new_width, new_height),)
thresh_image = np.zeros(resized_image.shape)
for i, threshold in enumerate(list_of_GRAYSCALE_threshold):
thresh_image[resized_image > threshold] = i
return thresh_image
if __name__ == "__main__":
image = cv2.imread("sob.png", 0)
Generated_ascii_image = image_to_ascii_symbol(image)
print_out_ascii(Generated_ascii_image)
Short Explanation to understand the code
import cv2
import numpy as np
list_of_symbols = ["#", "-", "*", ".", "+"]
list_of_RGB_threshold = [0, 50, 100, 150, 200]
list_of_GRAYSCALE_threshold=[0, 50, 100]
list_of_symbols[int(image_threshold_values) % len(list_of_symbols)]
height, width = image.shape
new_width = int(width / 20)
new_height = int(height / 40)
resized_image = cv2.resize(image, (new_width, new_height),)
np.zeros(resized_image.shape)
for i, threshold in enumerate(list_of_GRAYSCALE_threshold):
thresh_image[resized_image > threshold] = i
image = cv2.imread("sob.png", 0)