齐紫荆。 发表于 2020-4-4 10:53:31

图片变成字符的问题

from PIL import Image

def pic2ascii(pic, asciis, zoom, vscale):
    img = Image.open(pic)
    # 打开图片并转换为灰度模式
    out = img.convert("L")
    # 获取图片的宽度和高度
    width, height = out.size
    # 由于字符的宽度并不会等于高度,所以需要进行调整
    out = out.resize((int(width * zoom), int(height * zoom * vscale)))
    ascii_len = len(asciis)
    texts = ''

    for row in range(out.height):
      for col in range(out.width):
            gray = out.getpixel((col, row))
            texts += asciis
      texts += '\n'

    return texts

def main():
    pic = input("请输入待转换的图片名称:")
    # 10个字符表示按“灰度级别”从高到低排序
    asciis = "@%#*+=-:. "
    # 设置缩放系数
    zoom = 0.5
    # 设置垂直比例系数
    vscale = 0.5
    texts = pic2ascii(pic, asciis, zoom, vscale)
   
    with open(r"C:\Users\86182\Desktop\3.txt", "w") as file:
      file.write(texts)

if __name__ == "__main__":
    main()



我用的小甲鱼的代码,直接复制过来的,为什么转换不过来啊,那个路径也是前面不加r会报错

qiuyouzhi 发表于 2020-4-4 10:59:29

1,他那个图片都是有比例的,你得自己调一下
2,肯定要加r啊,Python学到这里了还是不懂吗?

齐紫荆。 发表于 2020-4-4 11:07:43

qiuyouzhi 发表于 2020-4-4 10:59
1,他那个图片都是有比例的,你得自己调一下
2,肯定要加r啊,Python学到这里了还是不懂吗?

知道要加r,只是看小甲鱼没加{:5_109:}
我调一下试试
页: [1]
查看完整版本: 图片变成字符的问题