Skip to content

Commit 0bb875b

Browse files
convert image to ascii
1 parent 9b9f59c commit 0bb875b

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

ascii_image/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This is a script to convert a image to ASCII charaters
2+
3+
- First install the requirements by ```pip install -r requirements.txt```
4+
- To Run use the following command
5+
6+
```python3 ascii_image.py "Path to input image" "Path to output.txt" -w W (width)(optional) -c(to invert the colour of image)(optional)```
7+
8+
- Run ```python3 ascii_image.py -h``` for help

ascii_image/ascii_image.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from PIL import Image
2+
import numpy as np
3+
import argparse
4+
5+
parser = argparse.ArgumentParser()
6+
7+
parser.add_argument('inputImage', help='Enter the path to image')
8+
parser.add_argument('outputFile', help='Enter the path to output File')
9+
parser.add_argument(
10+
'-w', '--width', help='Enter width of output image', type=int, default=75)
11+
parser.add_argument('-c', '--colorInvert',
12+
help='Enter to invert color of image', action='store_true')
13+
args = parser.parse_args()
14+
15+
16+
inputImagePath = args.inputImage
17+
outputPath = args.outputFile
18+
widd = args.width
19+
20+
asci = r"@%#*+=-:. "[::1]
21+
22+
if args.colorInvert:
23+
asci = r"@%#*+=-:. "[:: - 1]
24+
25+
# input image
26+
img = Image.open(inputImagePath)
27+
28+
wid, height = img.size
29+
img = img.resize((widd, int(widd*((height*9)/(wid*20)))))
30+
wid, height = img.size
31+
32+
img = img.convert("L")
33+
34+
35+
def avg(imggg):
36+
return (np.average(np.array(imggg)))
37+
38+
39+
# opening file
40+
f = open(outputPath, "w")
41+
42+
for j in range(height):
43+
for i in range(wid):
44+
img1 = img.crop((i, int(j), i + 1, int((j + 1))))
45+
f.write(asci[int((avg(img1) * 9)/255)])
46+
print(asci[int((avg(img1) * 9)/255)], end="")
47+
print("\n", end="")
48+
f.write("\n")
49+
50+
51+
f.close()

ascii_image/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
numpy==1.21.2
2+
Pillow==8.3.2

0 commit comments

Comments
 (0)