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:\Py\week\day01_22_hongzha\code\55.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