全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 3329|回复: 2

分享个有趣的脚本-图片转字符画

[复制链接]
发表于 2018-11-7 12:48:53 | 显示全部楼层 |阅读模式
已经兼容python2.7和python3
运行之后可以输入:图片链接或者本地图片绝对路径

  1. # -*- coding=utf-8 -*-
  2. from PIL import Image
  3. import sys
  4. import requests
  5. py3=False
  6. if sys.version_info[0]==2:
  7.     import StringIO
  8. else:
  9.     py3=True
  10.     import io as StringIO


  11. ASCII_CHARS = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft|()1{}[]?-_+~i!lI;:,^  "
  12. max_width=100  #调节此项可调节图片大小,越大图片信息越好。
  13. range_width=float(255)/(len(ASCII_CHARS)-1)

  14. def scale_image(image, new_width=max_width):
  15.     """Resizes an image preserving the aspect ratio.
  16.     """
  17.     (original_width, original_height) = image.size
  18.     aspect_ratio = original_height/float(original_width)*0.5
  19.     new_height = int(aspect_ratio * new_width)
  20.     new_image = image.resize((new_width, new_height))
  21.     return new_image

  22. def convert_to_grayscale(image):
  23.     return image.convert('L')

  24. def map_pixels_to_ascii_chars(image, range_width=range_width):
  25.     """Maps each pixel to an ascii char based on the range
  26.     in which it lies.
  27.     0-255 is divided into 11 ranges of 25 pixels each.
  28.     """
  29.     pixels_in_image = list(image.getdata())
  30.     pixels_to_chars = [ASCII_CHARS[int(pixel_value/range_width)] for pixel_value in pixels_in_image]
  31.     return "".join(pixels_to_chars)

  32. def convert_image_to_ascii(image, new_width=max_width):
  33.     image = scale_image(image)
  34.     image = convert_to_grayscale(image)
  35.     pixels_to_chars = map_pixels_to_ascii_chars(image)
  36.     len_pixels_to_chars = len(pixels_to_chars)
  37.     image_ascii = [pixels_to_chars[index: index + new_width] for index in range(0, len_pixels_to_chars, new_width)]
  38.     f=open('image_ascii.txt','w')
  39.     for line in image_ascii:
  40.         f.write('%s\n'%line)
  41.     return "\n".join(image_ascii)

  42. def handle_image_conversion(image_filepath):
  43.     image = None
  44.     session=requests.Session()
  45.     session.headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
  46.     if image_filepath.startswith('http://') or image_filepath.startswith('https://'):
  47.         resp=session.get(image_filepath)
  48.         if py3:
  49.             imagebuf=StringIO.BytesIO(resp.content)
  50.         else:
  51.             imagebuf=StringIO.StringIO(resp.content)
  52.         image=Image.open(imagebuf)
  53.     else:
  54.         image = Image.open(image_filepath)
  55.     image_ascii = convert_image_to_ascii(image)
  56.     print(image_ascii)

  57. if __name__=='__main__':
  58.     while 1:
  59.         print(u"请输入图片地址:")
  60.         try:
  61.             image_file_path = sys.argv[1]
  62.         except:
  63.             if py3:
  64.                 image_file_path=input()
  65.             else:
  66.                 image_file_path=raw_input()
  67.         handle_image_conversion(image_file_path)
复制代码
发表于 2018-11-8 02:02:54 来自手机 | 显示全部楼层
6666,学了两个月python还只会爬虫的我,看得自愧不如
发表于 2019-2-15 20:38:05 | 显示全部楼层
厉害了大佬
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-20 21:37 , Processed in 0.062585 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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