python图片转txt

在这里插入图片描述
图片前后比较,因为图片是晚上拍的,效果可能欠佳

from PIL import Image
img = Image.open("D:\PyCharm实例\zihuaxiang\IMG_20190513_215846-.png")
out = img.convert("L")
out.size
width, height = out.size
out = out.resize((int(width * 0.5),int(height * 0.5)))
width, height = out.size
print(width,height)
print(out.getpixel((100,100)))
print(out.getpixel((200,200)))
asciis = "@%#+=-. "
texts = " "
for row in range(height):
    for col in range(width):
        gray = out.getpixel((col,row))
        texts += asciis[int(gray / 255 * 2)]
    texts += "\n"
with open("D:/PyCharm实例/zihuaxiang/new2.txt","w") as file:
    file.write(texts)

https://blog.csdn.net/weixin_43206161

发布了68 篇原创文章 · 获赞 57 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43206161/article/details/104201607