马上国庆了,来波国庆头像烘托一下氛围,找这个实现代码也是磕磕绊绊,东拼西凑,为了方便大家使用,我直接封装好了,文末也附有实现所需的所有素材,包含py文件的代码以及字体包,有没有基础都能跑得了
效果图带有国旗发不出来
接下来我们来讲讲实现的方式吧!
首先环境准备,基础的python开发环境,PIL图片处理库,没有的话pip一下
pip install Pillow
然后我们介绍一下代码里字体位置怎么获取,
首先,从我的代码打包文件得到的字体,复制到C:\Windows\Fonts这个文件夹下,它会自动安装,安装完成之后找到这个字体,右键单击,进入属性/安全/对象名称就是了,复制出来,填到代码里就可以了
图片展示
到这里准备工作就完成了,把你的头像放到代码同级文件夹下,改一下代码里的文件名称,或者将你的头像文件名称改为1.jpg,就能跑了
国旗头像生成实现代码
def get_guoqitouxiang(touxiang_img,guoqi_img):
'''
这个函数可以完成头像与国旗的渐变融合,返回一个img对象
'''
key = 3.2 # 修改key值可以调整国旗的范围,推荐2~4之间的数字,支持小数
motherland_flag = Image.open(guoqi_img)
head_picture = Image.open(touxiang_img)
# 截图国旗上的五颗五角星
flag_width, flag_height = motherland_flag.size
crop_flag = motherland_flag.crop((66, 0, flag_height + 66, flag_height))
# 将国旗截图处理成颜色渐变
for i in range(flag_height):
for j in range(flag_height):
color = crop_flag.getpixel((i, j))
distance = int(math.sqrt(i * i + j * j))
alpha = 255 - int(distance // key)
new_color = (*color[0:-1], alpha if alpha > 0 else 0)
crop_flag.putpixel((i, j), new_color)
# 修改渐变图片的尺寸,适应头像大小,粘贴到头像上
new_crop_flag = crop_flag.resize(head_picture.size)
head_picture.paste(new_crop_flag, (0, 0), new_crop_flag)
# head_picture.save('国旗头像.png')
return head_picture
到这一步可以实现头像与国旗的渐变融合
!
接下来就是图片加文字,可以加任意文字,文字的大小、颜色、位置都是可以自行更改的,我这里加的是姓氏
实现代码:
def plus_word(touxiang_img,guoqi_img):
'''
这个函数可以实现在图片上写入文字,文字的颜色、大小,位置可以自行设置
'''
head_picture = get_guoqitouxiang(touxiang_img,guoqi_img)
print('国旗头像生成成功,正在写入文字...')
draw = ImageDraw.Draw(head_picture)
# 设置字体,如果没有,也可以不设置,通过修改这里的数字大小,改变文字的大小
font = ImageFont.truetype(r"C:\Users\smile25801\AppData\Local\Microsoft\Windows\Fonts\Aa福禄榜书.ttf", 140)
font1 = ImageFont.truetype(r"C:\Users\smile25801\AppData\Local\Microsoft\Windows\Fonts\Aa福禄榜书.ttf", 30)
# 通过改变第一个元组的数据,改变文字在图片中的位置,(x,y),图片左上角是(0,0),x轴向右增大,y轴向下增大
draw.text((710, 80), u"游", fill='#DC143C', font=font)
draw.text((710, 230), u"我爱我的国", fill='#DC143C', font=font1)
# head_picture.show()
now = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
head_picture.save(f'{now}国旗头像带姓氏.png')
print('头像保存成功')
到这里主要的功能函数就实现了,接下来定义一个主函数调用就行了
实现代码:
def run():
touxiang = '1.jpg'
guoqi = '国旗1024.png'
print('-----------------------开始执行-----------------------')
start = time.time()
plus_word(touxiang,guoqi)
end = time.time()
print(f'执行结束,用时{end-start}秒')
好,最后上一下完整代码和效果图
from PIL import Image,ImageDraw,ImageFont
import math,datetime
import time
def get_guoqitouxiang(touxiang_img,guoqi_img):
'''
这个函数可以完成头像与国旗的渐变融合,返回一个img对象
'''
key = 3.2 # 修改key值可以调整国旗的范围,推荐2~4之间的数字,支持小数
motherland_flag = Image.open(guoqi_img)
head_picture = Image.open(touxiang_img)
# 截图国旗上的五颗五角星
flag_width, flag_height = motherland_flag.size
crop_flag = motherland_flag.crop((66, 0, flag_height + 66, flag_height))
# 将国旗截图处理成颜色渐变
for i in range(flag_height):
for j in range(flag_height):
color = crop_flag.getpixel((i, j))
distance = int(math.sqrt(i * i + j * j))
alpha = 255 - int(distance // key)
new_color = (*color[0:-1], alpha if alpha > 0 else 0)
crop_flag.putpixel((i, j), new_color)
# 修改渐变图片的尺寸,适应头像大小,粘贴到头像上
new_crop_flag = crop_flag.resize(head_picture.size)
head_picture.paste(new_crop_flag, (0, 0), new_crop_flag)
# head_picture.save('国旗头像.png')
return head_picture
def plus_word(touxiang_img,guoqi_img):
'''
这个函数可以实现在图片上写入文字,文字的颜色、大小,位置可以自行设置
'''
head_picture = get_guoqitouxiang(touxiang_img,guoqi_img)
print('国旗头像生成成功,正在写入文字...')
draw = ImageDraw.Draw(head_picture)
# 设置字体,如果没有,也可以不设置,通过修改这里的数字大小,改变文字的大小
font = ImageFont.truetype(r"C:\Users\smile25801\AppData\Local\Microsoft\Windows\Fonts\Aa福禄榜书.ttf", 140)
font1 = ImageFont.truetype(r"C:\Users\smile25801\AppData\Local\Microsoft\Windows\Fonts\Aa福禄榜书.ttf", 30)
# 通过改变第一个元组的数据,改变文字在图片中的位置,(x,y),图片左上角是(0,0),x轴向右增大,y轴向下增大
draw.text((710, 80), u"游", fill='#DC143C', font=font)
draw.text((710, 230), u"我爱我的国", fill='#DC143C', font=font1)
# head_picture.show()
now = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
head_picture.save(f'{now}国旗头像带姓氏.png')
print('头像保存成功')
def run():
touxiang = '1.jpg'
guoqi = '国旗1024.png'
print('-----------------------开始执行-----------------------')
start = time.time()
plus_word(touxiang,guoqi)
end = time.time()
print(f'执行结束,用时{end-start}秒')
if __name__ == '__main__':
run()
传送门:完整代码及所有资料打包