Python带你生成个性海报头像
利用Python语言,可以很方便帮你制作个性海报头像,无需插件,纯代码制作生成。
一、生成文字型海报头像
要生成一张关于Python基础语法的海报图片,你可以使用Python中的库如PIL(Pillow)来处理图像。
1.1安装依赖
首先安装PIL依赖:
pip install Pillow
我已经安装过了,无需再安装。
由于海报需要背景图,根据网络背景图url来生成背景,故需要安装request库:
pip install requests
博主已安装过。
安装完成后,你可以通过运行以下 Python 脚本来验证 requests 是否成功安装:
import requests
response = requests.get('https://httpbin.org/get')
print(response.status_code)
print(response.text)
新建test_requests.py 文件,拷贝以上代码:
右键运行
以下结果表示安装requests成功:
1.2编写生成海报代码
导入库:
# 导入所需的库
from PIL import Image, ImageDraw, ImageFont
import requests
from io import BytesIO
定义函数下载图片:
# 定义函数来下载图片
def download_image(url):
response = requests.get(url)
image = Image.open(BytesIO(response.content))
return image
计算居中位置:
# 计算居中位置
def get_centered_position(draw, text, font):
left, top, right, bottom = draw.textbbox((0, 0), text, font=font)
text_width = right - left
text_height = bottom - top
image_width, image_height = draw.im.size
x = (image_width - text_width) // 2
y = (image_height - text_height) // 2
return x, y
定义函数创建海报:
# 定义函数来创建海报
def create_poster(background_url, text, font_path, output_path):
# 下载背景图片
background = download_image(background_url)
# 创建绘图对象
draw = ImageDraw.Draw(background)
# 设置字体
font = ImageFont.truetype(font_path, size=40)
# 设置文本颜色
text_color = (0, 128, 0) # 深绿色
# 计算居中位置
text_position = get_centered_position(draw, text, font)
# 绘制文本
draw.text(text_position, text, fill=text_color, font=font)
# 保存海报
background.save(output_path)
编写主程序,设置图片网络url:
# 主程序入口
if __name__ == "__main__":
# 背景图片URL
background_url = "https://img1.wallspic.com/previews/3/6/2/6/7/176263/176263-fan_she-luo_fu_dun-shui_zi_yuan-yu_hui-mu-x750.jpg"
# 文本内容
text = "Python基础学习\n\n1. 变量声明\n2. 数据类型\n3. 控制结构\n4. 函数定义\n5. 模块与包\n6. 异常处理"
# 字体路径
font_path = "simhei.ttf" # 替换为支持中文的字体文件路径
# 输出路径
output_path = "poster.png"
# 创建海报
create_poster(background_url, text, font_path, output_path)
print(f"海报已保存到 {output_path}")
其中背景图片url,可以打开百度查找:
打开一个海报图片网站:
选择一张背景图片:
右键检查元素,查看图片url:
将图片url复制到Python程序背景图:
替换后,右键运行Python程序,完整代码:
# 导入所需的库
from PIL import Image, ImageDraw, ImageFont
import requests
from io import BytesIO
# 定义函数来下载图片
def download_image(url):
response = requests.get(url)
image = Image.open(BytesIO(response.content))
return image
# 计算居中位置
def get_centered_position(draw, text, font):
left, top, right, bottom = draw.textbbox((0, 0), text, font=font)
text_width = right - left
text_height = bottom - top
image_width, image_height = draw.im.size
x = (image_width - text_width) // 2
y = (image_height - text_height) // 2
return x, y
# 定义函数来创建海报
def create_poster(background_url, text, font_path, output_path):
# 下载背景图片
background = download_image(background_url)
# 创建绘图对象
draw = ImageDraw.Draw(background)
# 设置字体
font = ImageFont.truetype(font_path, size=40)
# 设置文本颜色
text_color = (0, 128, 0) # 深绿色
# 计算居中位置
text_position = get_centered_position(draw, text, font)
# 绘制文本
draw.text(text_position, text, fill=text_color, font=font)
# 保存海报
background.save(output_path)
# 主程序入口
if __name__ == "__main__":
# 背景图片URL
background_url = "https://img1.wallspic.com/previews/3/6/2/6/7/176263/176263-fan_she-luo_fu_dun-shui_zi_yuan-yu_hui-mu-x750.jpg"
# 文本内容
text = "Python基础语法\n\n1. 变量声明\n2. 数据类型\n3. 控制结构\n4. 函数定义\n5. 模块与包\n6. 异常处理"
# 字体路径
font_path = "simhei.ttf" # 替换为支持中文的字体文件路径
# 输出路径
output_path = "poster.png"
# 创建海报
create_poster(background_url, text, font_path, output_path)
print(f"海报已保存到 {output_path}")
出现海报已经保存:
点击查看,当前目录出现poster.png:
图片样式:
这样就生成了文字型背景海报。
二、生成图片型海报
2.1准备背景图
将刚才生成的poster.png背景图放入程序目录:
然后编写generate_pic2.py文件:
2.2详细代码
导入包:
from PIL import Image, ImageDraw, ImageFont
定义海报参数:
# 定义海报的基本参数
width, height = 800, 1200
background_image_path = 'poster.png' # 背景图片路径
创建图像对象:
# 创建一个新的图像对象
image = Image.new('RGB', (width, height), color='white')
# 打开背景图片并调整大小
bg_image = Image.open(background_image_path).resize((width, height))
image.paste(bg_image, (0, 0))
准备绘图对象:
# 准备绘图对象
draw = ImageDraw.Draw(image)
加载字体:
# 加载字体
font_path_title = 'simhei.ttf' # 标题字体路径
font_path_text = 'simhei.ttf' # 内容字体路径
title_font = ImageFont.truetype(font_path_title, 48)
text_font = ImageFont.truetype(font_path_text, 24)
获取标题尺寸:
# 获取标题尺寸
title_text = "Python 基础语法"
title_bbox = draw.textbbox((0, 0), title_text, font=title_font)
title_width = title_bbox[2] - title_bbox[0]
title_height = title_bbox[3] - title_bbox[1]
draw.text(((width - title_width) / 2, 50), title_text, fill='black', font=title_font)
添加内容:
# 添加内容
content = [
"变量与数据类型:",
"int, float, str, bool",
"条件语句:",
"if condition:",
" pass",
"循环:",
"for item in iterable:",
" pass",
"函数定义:",
"def function_name(args):",
" pass",
]
绘制头像:
y_position = 150
for line in content:
text_bbox = draw.textbbox((0, 0), line, font=text_font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
draw.text(((width - text_width) / 2, y_position), line, fill='black', font=text_font)
y_position += text_height + 10
保存头像:
# 保存图像
image.save('python_syntax_poster.png')
print("海报已生成!")
完整代码:
from PIL import Image, ImageDraw, ImageFont
# 定义海报的基本参数
width, height = 800, 1200
background_image_path = 'poster.png' # 背景图片路径
# 创建一个新的图像对象
image = Image.new('RGB', (width, height), color='white')
# 打开背景图片并调整大小
bg_image = Image.open(background_image_path).resize((width, height))
image.paste(bg_image, (0, 0))
# 准备绘图对象
draw = ImageDraw.Draw(image)
# 加载字体
font_path_title = 'simhei.ttf' # 标题字体路径
font_path_text = 'simhei.ttf' # 内容字体路径
title_font = ImageFont.truetype(font_path_title, 48)
text_font = ImageFont.truetype(font_path_text, 24)
# 获取标题尺寸
title_text = "Python 基础语法"
title_bbox = draw.textbbox((0, 0), title_text, font=title_font)
title_width = title_bbox[2] - title_bbox[0]
title_height = title_bbox[3] - title_bbox[1]
draw.text(((width - title_width) / 2, 50), title_text, fill='black', font=title_font)
# 添加内容
content = [
"变量与数据类型:",
"int, float, str, bool",
"条件语句:",
"if condition:",
" pass",
"循环:",
"for item in iterable:",
" pass",
"函数定义:",
"def function_name(args):",
" pass",
]
y_position = 150
for line in content:
text_bbox = draw.textbbox((0, 0), line, font=text_font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
draw.text(((width - text_width) / 2, y_position), line, fill='black', font=text_font)
y_position += text_height + 10
# 保存图像
image.save('python_syntax_poster.png')
print("海报已生成!")
右键运行:
生成海报:
本文分享了用Python生成个性海报的方法,更多AI技术资讯欢迎关注我。