|  | 
 
| 
from PIL import Image
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  
 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[int((gray / 255) * (ascii_len - 1))]
 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会报错
 
1,他那个图片都是有比例的,你得自己调一下2,肯定要加r啊,Python学到这里了还是不懂吗?
 | 
 |