用户:
tom_z查看:0 回复:1 评论:0 创建时间:2020-12-23T16:18:21
【作品展示】

【作品介绍】
字符风格转化
【作品源代码】
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt
#图片宽高
width, height = (240, 240)
#灰度图
img = Image.open("LiuLiu.jpg").convert("L").resize((width, height))
#plt.imshow(img)
#plt.show()
ASCII_HIGH = '''$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. '''
txt = ''
for y in range(height):
for x in range(width):
pos = (x, y)
gray = img.getpixel(pos) #0~255的亮度值
index = int(gray/256*70)
txt += ASCII_HIGH[index]+'@'
txt += '\n'
#print(txt)
#with open("bcm.txt",'w') as f:
# f.write(txt)
#字符串转图片
img_new = Image.new("RGB", (width*12, height*15), "white") # 生成空白图片
draw = ImageDraw.Draw(img_new) # 图片转画布
draw.text((0, 0), txt, fill="black") # 文字写入
img_new.resize((width*12, height*12)).save("LiuLiu_SoCute.jpg")
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn