鱼C论坛

 找回密码
 立即注册
查看: 2316|回复: 6

图像转txt,小白

[复制链接]
发表于 2020-2-13 15:27:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
怎样可以将一张图片用不同的字符表示,并生成一个txt文件,在notepad++上打开缩小,使得看起来和原来的图片差不多
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-13 15:38:22 | 显示全部楼层
网上搜一下字符画,比如https://www.cnblogs.com/wjw1014/p/10318544.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-13 15:39:58 | 显示全部楼层
  1. from PIL import Image

  2. def convert(pic, asciis, zoom, vscale):
  3.     img = Image.open(pic)

  4.     out = img.convert("L")
  5.     width, height = out.size
  6.    
  7.     if height >= 800 and width >= 800:
  8.         out = out.resize((int(width * zoom), int(height * zoom * vscale)))
  9.     elif height >= 800 and width < 800:
  10.         out = out.resize((int(width), int(height * zoom * vscale)))
  11.     elif height < 800 and width >= 800:
  12.         out = out.resize((int(width * zoom), int(height * vscale)))
  13.    
  14.     ascii_len = len(asciis)
  15.     texts =""

  16.     for row in range(out.height):
  17.         for col in range(out.width):
  18.             gray = out.getpixel((col, row))
  19.             texts += asciis[int((gray / 255) * (ascii_len - 1))]
  20.         texts += '\n'

  21.     return texts

  22. def main():
  23.     pic = input("请输入图片路径:")
  24.    
  25.     asciis = "@%#*+=-:. "
  26.     zoom = 0.5
  27.     vscale = 0.5
  28.     texts = convert(pic, asciis, zoom, vscale)
  29.    
  30.     with open("字符画.txt", "w") as file:
  31.         file.write(texts)

  32. if __name__ == "__main__":
  33.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-13 15:55:52 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-13 16:11:40 | 显示全部楼层
冬雪雪冬 发表于 2020-2-13 15:38
网上搜一下字符画,比如https://www.cnblogs.com/wjw1014/p/10318544.html

谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-13 16:13:09 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-13 16:16:14 | 显示全部楼层
zltzlt 发表于 2020-2-13 15:55
https://fishc.com.cn/thread-123382-1-1.html

谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-22 10:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表