forked from jzlikewei/img2ascii
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg2ascii.py
27 lines (27 loc) · 896 Bytes
/
img2ascii.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
from PIL import Image
class Img2ascii:
chars=[' ', ',', '+', '1', 'n','D','&','M']
def getchar(self,pi):
for i in range(0,8):
if pi< (i+1)*32:
return self.__class__.chars[7-i]
def __init__(self,src,resize=1.0):
img = Image.open(src)
if img.mode=='P' or img.mode =='RGBA':
im=Image.new('RGB',img.size,'white')
im.paste(img.convert('RGBA'),img.convert('RGBA'))
img= im
img= img.convert('L')
w,h =img.size
h/=2
w=int(w*resize)
h=int(h*resize)
img=img.resize((w,h),Image.ANTIALIAS)
#img.save('tmp.jpg')
pixs = img.load()
self.data=[]
for i in range(0,h):
line =''
for j in range(0,w):
line+=self.getchar(pixs[j,i])
self.data.append(line)