Image to ASCII - A Python Projects for Beginners Practice

image to ascii

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)


Input for this code: We used the logo of "School of beginners" as the input and named it "sob.png".


School of Beginners Logo



Output for this code: The output is a series of symbols that is captured and shown below. First output for "list_of_GRAYSCALE_threshold=[0, 50, 100]" and second output for "list_of_GRAYSCALE_threshold= [0, 100, 200, 300, 400]".

image to ascii output


image to ascii output



Short Explanation to understand the code


Import all required python projects
import cv2
import numpy as np
Declaring list of symbols and list os thresholds.
list_of_symbols = ["#", "-", "*", ".", "+"]
list_of_RGB_threshold = [0, 50, 100, 150, 200]
list_of_GRAYSCALE_threshold=[0, 50, 100]
Selecting symbols through coding.
list_of_symbols[int(image_threshold_values) % len(list_of_symbols)]
Adjusting height and width to fit the image on the screen
height, width = image.shape
new_width = int(width / 20)
new_height = int(height / 40)
Resize the input image to fit on the output screen.
resized_image = cv2.resize(image, (new_width, new_height),)
Thresholding of an image for further process
np.zeros(resized_image.shape)
Assigning values from the list of symbols with the list of thresholds using image data
for i, threshold in enumerate(list_of_GRAYSCALE_threshold):
thresh_image[resized_image > threshold] = i
Reading the "School of Beginners" image as input in the grayscale mode "0".
image = cv2.imread("sob.png", 0)

Read Machine Learning Projects in python:

Thanks to Github for this post

Post a Comment

Previous Post Next Post