python图片转字符画
文章目录
解析
from PIL import Image#从模块导入Image,导入需要的图像库
from PIL import ImageDraw#从模块导入ImageDraw,提供了图像对象的简单2D绘制
from PIL import ImageFont#从模块导入ImageFomt #定义了相同名称的类
import matplotlib.pyplot as plt#导入模块,创建别名,有命令风格的函数集合
import numpy as np#导入模块,创建别名 开源的数值计算扩展工具
import time#导入时间模块
def pic_to_words(srd_img_file_path, dst_img_file_path=None, scale=2, sample_step=3):#定义函数
start_time = int(time.time())#返回当前时间的时间戳(1970纪元后经过的浮点秒数)。
# 读取图片信息
old_img = Image.open(srd_img_file_path)#读取图片
pix = old_img.load()#从文件句柄中打开文件,加载到Python的变量中
width = old_img.size[0]#得到图片尺寸
height = old_img.size[1]
print("width:%d, height:%d" % (width, height))
# 创建新图片
canvas = np.ndarray((height*scale, width*scale, 3), np.uint8)
canvas[:, :, :] = 255
new_image = Image.fromarray(canvas)#实现array到image的转换
draw = ImageDraw.Draw(new_image)#创建一个可以在给定图像上绘图的对象。
# 创建绘制对象
font = ImageFont.truetype("consola.ttf", 10, encoding="unic")#创建字体对象
char_table = list('520 ')#设置文字内容
# 开始绘制
pix_count = 0
table_len = len(char_table)#返回对象长度
for y in range(height):
for x in range(width):#遍历图片
if x % sample_step == 0 and y % sample_step == 0:
draw.text((x*scale, y*scale),
char_table[pix_count % table_len], pix[x, y], font)
pix_count += 1#一次加一
# 保存
if dst_img_file_path is not None:
new_image.save(dst_img_file_path)#进行图片保存
print("used time : %d second, pix_count : %d" %
((int(time.time()) - start_time), pix_count))
print(pix_count)
new_image.show()#显示图片
INPUT = "C://Users/admin/shiyanlou/artistic.png"#输入图片
OUTPUT= "C://Users/admin/shiyanlou/output.jpg"#输出字符画
pic_to_words(INPUT,OUTPUT)#调用函数
示例输出字符画:

完整代码
from PIL import Image#从模块导入Image,导入需要的图像库
from PIL import ImageDraw#从模块导入ImageDraw,提供了图像对象的简单2D绘制
from PIL import ImageFont#从模块导入ImageFomt #定义了相同名称的类
import matplotlib.pyplot as plt#导入模块,创建别名,有命令风格的函数集合
import numpy as np#导入模块,创建别名 开源的数值计算扩展工具
import time#导入时间模块
def pic_to_words(srd_img_file_path, dst_img_file_path=None, scale=2, sample_step=3):#定义函数
start_time = int(time.time())#返回当前时间的时间戳(1970纪元后经过的浮点秒数)。
# 读取图片信息
old_img = Image.open(srd_img_file_path)#读取图片
pix = old_img.load()#从文件句柄中打开文件,加载到Python的变量中
width = old_img.size[0]#得到图片尺寸
height = old_img.size[1]
print("width:%d, height:%d" % (width, height))
# 创建新图片
canvas = np.ndarray((height*scale, width*scale, 3), np.uint8)
canvas[:, :, :] = 255
new_image = Image.fromarray(canvas)#实现array到image的转换
draw = ImageDraw.Draw(new_image)#创建一个可以在给定图像上绘图的对象。
# 创建绘制对象
font = ImageFont.truetype("consola.ttf", 10, encoding="unic")#创建字体对象
char_table = list('520 ')#设置文字内容
# 开始绘制
pix_count = 0
table_len = len(char_table)#返回对象长度
for y in range(height):
for x in range(width):#遍历图片
if x % sample_step == 0 and y % sample_step == 0:
draw.text((x*scale, y*scale),
char_table[pix_count % table_len], pix[x, y], font)
pix_count += 1#一次加一
# 保存
if dst_img_file_path is not None:
new_image.save(dst_img_file_path)#进行图片保存
print("used time : %d second, pix_count : %d" %
((int(time.time()) - start_time), pix_count))
print(pix_count)
new_image.show()#显示图片
INPUT = "C://Users/admin/shiyanlou/artistic.png"
OUTPUT= "C://Users/admin/shiyanlou/output.jpg"
pic_to_words(INPUT,OUTPUT)#调用函数
博客主要介绍Python图片转字符画,包括解析部分,如导入所需图像库、读取图片信息、创建新图片、绘制文字等步骤,还给出完整代码。此外,还附带Python制作二维码、爬取整本小说、天气预报、百度图片等简易步骤。
2270

被折叠的 条评论
为什么被折叠?



