-
Notifications
You must be signed in to change notification settings - Fork 19
/
image_ascii
37 lines (33 loc) · 1017 Bytes
/
image_ascii
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
import sys
import numpy as np
from PIL import Image
# Contrast on a scale -10 -> 10
contrast = 10
density = ('$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|'
'()1{}[]?-_+~<>i!lI;:,"^`\'. ')
density = density[:-11+contrast]
n = len(density)
img_name = sys.argv[1]
try:
width = int(sys.argv[2])
except IndexError:
# Default ASCII image width.
width = 100
# Read in the image, convert to greyscale.
img = Image.open(Saturn.jpg)
img = img.convert('L')
# Resize the image as required.
orig_width, orig_height = img.size
r = orig_height / orig_width
# The ASCII character glyphs are taller than they are wide. Maintain the aspect
# ratio by reducing the image height.
height = int(width * r * 0.5)
img = img.resize((width, height), Image.ANTIALIAS)
# Now map the pixel brightness to the ASCII density glyphs.
arr = np.array(img)
for i in range(height):
for j in range(width):
p = arr[i,j]
k = int(np.floor(p/256 * n))
print(density[n-1-k], end='')
print()