Skip to content

Commit 29f8385

Browse files
Ascii art generation
Project to generate art using Ascii characters #hactoberfest2022
1 parent b933a6f commit 29f8385

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

Ascii_art/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ascii-art
2+
3+
## Description
4+
This Project will take an 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 [make_art.py](https://github.com/Shiny-Akash/python-mini-projects/blob/ascii-art/projects/asciiart/make_art.py) .
11+
In the command line, Enter
12+
13+
`python3 make_art.py [image_path]`
14+
15+
Replace the `[image_path]` with the image you want to do ascii-art. By default it takes [sample_image.png](https://github.com/Shiny-Akash/python-mini-projects/blob/ascii-art/projects/asciiart/sample_image.png)
16+
17+
## Customization
18+
There are two things you can customize in [make_art.py](https://github.com/Shiny-Akash/python-mini-projects/blob/ascii-art/projects/asciiart/make_art.py).
19+
20+
* `symbols_list`
21+
* `threshold_list`
22+
23+
Modify the threshold list to take **different threshold values** thereby changing the patterns in the printed image.
24+
Modify the symbols list to substitute **different symbols** in the coded patterns.
25+
26+
## Sample
27+
Input Image :
28+
![input_image](sample_image.png)
29+
Output :
30+
![output_image](sample_output.png)

Ascii_art/make_art.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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)

Ascii_art/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
opencv-python==4.3.0.36
2+
numpy==1.19.1

Ascii_art/sample_image.png

48.6 KB
Loading

Ascii_art/sample_output.png

6.31 KB
Loading

0 commit comments

Comments
 (0)