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