介绍
这篇文章主要介绍了python怎么将图片转换为字符图片,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获、下面让小编带着大家一起了解一下。
<强>简介强>
一个简单的python程序,将图片转换为字符图片。
<强>正文强>
原图(侵删)
结果图
源码
[更多细节)——在戳这里
# - *安康;编码:,UTF-8 - *安康; 得到PIL import  Image 得到PIL import  ImageDraw 得到PIL import  ImageFont import matplotlib.pyplot  as plt import numpy  as np import 时间 def happyNewYear (srd_img_file_path, dst_img_file_path =,,, scale =, 2, sample_step =, 3): ,start_time =, int (time.time ()) ,#读取图片信息=,,old_img  Image.open (srd_img_file_path)=,,pix  old_img.load ()=,,width  old_img.size [0]=,,height  old_img.size [1] ,print (“宽度:% d,高度:% d", %,(宽度,高度)) ,#创建新图片=,,canvas  np.ndarray(高*,,宽度*规模,,3),,np.uint8) ,画布(:,,,,,:],=255=,,new_image  Image.fromarray(帆布)=,,draw  ImageDraw.Draw (new_image) ,#创建绘制对象=,,font  ImageFont.truetype (“consola.ttf",, 10日,编码=皍nic")=,char_table 列表(& # 39;happy  new year & # 39;) ,# font =, ImageFont.truetype (& # 39; simsun.ttc& # 39;,, 10) ,# char_table =,列表(u # 39;新年快乐& # 39;) ,#开始绘制 pix_count =, 0=,table_len , len (char_table) ,for y 拷贝范围(高度): for 才能;x 拷贝范围(宽度): ,,if x %, sample_step ==, 0,以及y %, sample_step ==, 0: ,,,draw.text ((x *规模,,y *规模),,char_table [pix_count %, table_len],,照片(x, y),字体) ,,,pix_count +=1 ,#保存 ,if dst_img_file_path  is not 没有: new_image.save才能(dst_img_file_path) ,打印(“used time : % d 第二,,pix_count :, % d", %, ((int (time.time()),安康;start_time),, pix_count)) ,打印(pix_count) ,new_image.show () happyNewYear (“input.jpg",,“output.jpg")
感谢你能够认真阅读完这篇文章,希望小编分享的“python怎么将图片转换为字符图片”这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!