|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import cv2 |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +import sys |
| 7 | + |
| 8 | +symbols_list = ["#", "-", "*", ".", "+", "o"] |
| 9 | +threshold_list = [0, 50, 100, 150, 200] |
| 10 | + |
| 11 | +def print_out_ascii(array): |
| 12 | + """prints the coded image with symbols""" |
| 13 | + |
| 14 | + for row in array: |
| 15 | + for e in row: |
| 16 | + # select symbol based on the type of coding |
| 17 | + print(symbols_list[int(e) % len(symbols_list)], end="") |
| 18 | + print() |
| 19 | + |
| 20 | + |
| 21 | +def img_to_ascii(image): |
| 22 | + """returns the numeric coded image""" |
| 23 | + |
| 24 | + # resizing parameters |
| 25 | + # adjust these parameters if the output doesn't fit to the screen |
| 26 | + height, width = image.shape |
| 27 | + new_width = int(width / 20) |
| 28 | + new_height = int(height / 40) |
| 29 | + |
| 30 | + # resize image to fit the printing screen |
| 31 | + resized_image = cv2.resize(image, (new_width, new_height),) |
| 32 | + |
| 33 | + thresh_image = np.zeros(resized_image.shape) |
| 34 | + |
| 35 | + for i, threshold in enumerate(threshold_list): |
| 36 | + # assign corresponding values according to the index of threshold applied |
| 37 | + thresh_image[resized_image > threshold] = i |
| 38 | + return thresh_image |
| 39 | + |
| 40 | + |
| 41 | +if __name__ == "__main__": |
| 42 | + |
| 43 | + if len(sys.argv) < 2: |
| 44 | + print("Image Path not specified : Using sample_image.png\n") |
| 45 | + image_path = "sample_image.png" # default image path |
| 46 | + |
| 47 | + if len(sys.argv) == 2: |
| 48 | + print("Using {} as Image Path\n".format(sys.argv[1])) |
| 49 | + image_path = sys.argv[1] |
| 50 | + |
| 51 | + image = cv2.imread(image_path, 0) # read image |
| 52 | + |
| 53 | + ascii_art = img_to_ascii(image) |
| 54 | + print_out_ascii(ascii_art) |
0 commit comments