python补充符号和图片_Python 图片转符号图

006tNc79gy1ffwumrqg9uj30qk0hstcb.jpg

先上图,如果用的是 mac 的时候打开 txt 文件时候 com + -图就可以清楚的显示了

006tNc79gy1ffwuilq31nj312c0yqe2f.jpg

006tNc79gy1ffwug730fcj30ya1781he.jpg

下面上代码吧#!/usr/bin/python

# -*- coding: UTF-8 -*-

from PIL import Image

import os

serarr = '''@#$%&?*aeoc=

count = len(serarr)

def toText(image_file): # 此函数不能输入 gif 文件

image_file = image_file.convert("L") # 转灰度

asd = '' # 储存字符串

for h in range(0, image_file.size[1]): # h

for w in range(0, image_file.size[0]): # w

gray = image_file.getpixel((w, h))

asd = asd + serarr[int(gray / (256 / (count)))]

asd = asd + 'rn'

return asd

def toText2(image_file):

asd = '' # 储存字符串

for h in range(0, image_file.size[1]): # h

for w in range(0, image_file.size[0]): # w

r, g, b = image_file.getpixel((w, h))

gray = int(r * 0.299 + g * 0.587 + b * 0.114)

asd = asd + serarr[int(gray / (256 / (count)))]

asd = asd + 'rn'

return asd

image_file = Image.open("Unknown.jpg") # 打开图片

image_file = image_file.resize((int(image_file.size[0] * 0.9), int(image_file.size[1] * 0.5))) # 调整图片大小

print

u'Info:', image_file.size[0], ' ', image_file.size[1], ' ', count

tmp = open('tmp.txt', 'w')

tmp.write(toText(image_file))

tmp.close()

用 PyCharm 好用些

图形化界面还没搞出来,下次

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值