import PIL
from PIL import Image
def get_bin_table(threshold=155):
'''
获取灰度转二值的映射table
0表示黑色,1表示白色
'''
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
return table
im = Image.open(r"D:Pyweekday01_22_hongzhacode55.jpg")
im2 = im.convert("L")
# im2.show()
table = get_bin_table()
binary = im2.point(table, '1')
a = list(binary.getdata())
print(a)
width,height = binary.size
start = 0
step = width
# 直接放在控制台输出
for i in range(height):
for p in a[start:start+step]:
if p==1:
p =' '
print(p,end='')
print()
start+=step