立即注册  找回密码
 立即注册
CeraNetworksBGVM服务器主机交流会员请立即修改密码Sharktech防护
查看: 405|回复: 0

分享个有趣的脚本

[复制链接]

分享个有趣的脚本

[复制链接]

119

主题

447

回帖

2570

积分

金牌会员

积分
2570
Abbey

119

主题

447

回帖

2570

积分

金牌会员

积分
2570
2018-11-7 12:48:53 | 显示全部楼层 |阅读模式
已经兼容python2.7和python3
运行之后可以输入:图片链接或者本地图片绝对路径
[ol]
  • # -*- coding=utf-8 -*-
  • from PIL import Image
  • import sys
  • import requests
  • py3=False
  • if sys.version_info[0]==2:
  •     import StringIO
  • else:
  •     py3=True
  •     import io as StringIO
  • ASCII_CHARS = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft|()1{}[]?-_+~i!lI;:,^  "
  • max_width=100  #调节此项可调节图片大小,越大图片信息越好。
  • range_width=float(255)/(len(ASCII_CHARS)-1)
  • def scale_image(image, new_width=max_width):
  •     """Resizes an image preserving the aspect ratio.
  •     """
  •     (original_width, original_height) = image.size
  •     aspect_ratio = original_height/float(original_width)*0.5
  •     new_height = int(aspect_ratio * new_width)
  •     new_image = image.resize((new_width, new_height))
  •     return new_image
  • def convert_to_grayscale(image):
  •     return image.convert('L')
  • def map_pixels_to_ascii_chars(image, range_width=range_width):
  •     """Maps each pixel to an ascii char based on the range
  •     in which it lies.
  •     0-255 is divided into 11 ranges of 25 pixels each.
  •     """
  •     pixels_in_image = list(image.getdata())
  •     pixels_to_chars = [ASCII_CHARS[int(pixel_value/range_width)] for pixel_value in pixels_in_image]
  •     return "".join(pixels_to_chars)
  • def convert_image_to_ascii(image, new_width=max_width):
  •     image = scale_image(image)
  •     image = convert_to_grayscale(image)
  •     pixels_to_chars = map_pixels_to_ascii_chars(image)
  •     len_pixels_to_chars = len(pixels_to_chars)
  •     image_ascii = [pixels_to_chars[index: index + new_width] for index in range(0, len_pixels_to_chars, new_width)]
  •     f=open('image_ascii.txt','w')
  •     for line in image_ascii:
  •         f.write('%s\n'%line)
  •     return "\n".join(image_ascii)
  • def handle_image_conversion(image_filepath):
  •     image = None
  •     session=requests.Session()
  •     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'}
  •     if image_filepath.startswith('http://') or image_filepath.startswith('https://'):
  •         resp=session.get(image_filepath)
  •         if py3:
  •             imagebuf=StringIO.BytesIO(resp.content)
  •         else:
  •             imagebuf=StringIO.StringIO(resp.content)
  •         image=Image.open(imagebuf)
  •     else:
  •         image = Image.open(image_filepath)
  •     image_ascii = convert_image_to_ascii(image)
  •     print(image_ascii)
  • if __name__=='__main__':
  •     while 1:
  •         print(u"请输入图片地址:")
  •         try:
  •             image_file_path = sys.argv[1]
  •         except:
  •             if py3:
  •                 image_file_path=input()
  •             else:
  •                 image_file_path=raw_input()
  •         handle_image_conversion(image_file_path)
  • [/ol]复制代码
  • 回复

    使用道具 举报

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

    本版积分规则

    Archiver|小黑屋|HS2V主机综合交流论坛

    GMT+8, 2024-12-27 21:22 , Processed in 0.013670 second(s), 2 queries , Gzip On, Redis On.

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

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