Skip to content

Commit 574eecb

Browse files
Merge pull request Mayank94043626#76 from Mayank9404/master
ascii art
2 parents 069e92e + 6aa423d commit 574eecb

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

Projects/ascii-art/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# ASCII-ART
2+
3+
## Description
4+
This Python Project will take a simple image and print out an [ASCII-art](https://en.wikipedia.org/wiki/ASCII_art).
5+
6+
## About this Project
7+
This project uses [opencv](https://www.opencv.org) to process image and [numpy](https://numpy.org) to manipulate arrays. The Image is numerically coded for different threshold regions it comes under and for each coded regions, a symbol is used for printing it out in the stdout.
8+
9+
## Usage
10+
Use the Script create_art.py .
11+
In the command line, Enter
12+
13+
`python3 create_art.py [image_path]`
14+
15+
Replace the `[image_path]` with the image you want to do ascii-art.

Projects/ascii-art/create_art.py

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

0 commit comments

Comments
 (0)