自动批量生成图片代码
我自己使用的。你参考
from PIL import Image, ImageDraw, ImageFont
import os, sys,datetime
# 获取当前文件所在的绝对路径
current_dir = os.path.dirname(os.path.abspath(__file__))
# 获取当前文件的父目录
parent_dir = os.path.dirname(current_dir)
# 将根目录添加到系统路径中
sys.path.append(parent_dir)
from config import *def generate_image_750(text):width, height = 750, 750background_color = (255, 255, 255) # 白色背景text_color = (0, 0, 0) # 黑色字体font_size = 72font_path = r'C:\Windows\Fonts\simhei.ttf'image = Image.new('RGB', (width, height), background_color)draw = ImageDraw.Draw(image)font = ImageFont.truetype(font_path, font_size)# 使用 textbbox 获取文本的边界框left, top, right, bottom = draw.textbbox((0, 0), text, font=font)text_width = right - lefttext_height = bottom - top# 计算文本的中心位置x = (width - text_width) // 2y = (height - text_height) // 2draw.text((x, y), text, fill=text_color, font=font)# 保存图像image.save(f'../pngs/{text}.png')image.show()def generate_image_750_auto_text(text, bg=None, title="佩玉宝宝故事书"):width, height = 768, 768if bg == "black":background_color = (255, 255, 255) # 白色背景text_color = (0, 0, 0) # 黑色字体title_color = (0, 0, 0) # 黑色标题elif bg == "darkblue":background_color = (0, 0, 128) # 深蓝色背景text_color = (255, 255, 255) # 白色字体title_color = (255, 255, 255) # 白色标题elif bg == "lightgray":background_color = (240, 240, 240) # 浅灰色背景text_color = (64, 64, 64) # 深灰色字体title_color = (64, 64, 64) # 深灰色标题else:background_color = (255, 255, 204) # 浅黄色背景text_color = (0, 100, 0) # 深绿色字体title_color = (0, 0, 128) # 深蓝色标题font_path = r'C:\Windows\Fonts\simhei.ttf'margin = 50 # 左右留空的宽度image = Image.new('RGB', (width, height), background_color)draw = ImageDraw.Draw(image)# 自动调整字体大小以适应指定区域max_font_size = 100 # 最大字体大小min_font_size = 10 # 最小字体大小font_size = max_font_sizetext_width, text_height = width - 2 * margin + 1, height + 1 # 初始化为大于指定区域的值font = None # 初始化 font 变量while text_width > width - 2 * margin or text_height > height:font_size -= 1if font_size < min_font_size:breakfont = ImageFont.truetype(font_path, font_size)left, top, right, bottom = draw.textbbox((0, 0), text, font=font)text_width = right - lefttext_height = bottom - top# 确保 font 被正确赋值if font is None:font = ImageFont.truetype(font_path, min_font_size)left, top, right, bottom = draw.textbbox((0, 0), text, font=font)text_width = right - lefttext_height = bottom - top# 计算每行文本的高度title = titlecontent = texttitle_width, title_height = draw.textbbox((0, 0), title, font=font)[2:4]content_width, content_height = draw.textbbox((0, 0), content, font=font)[2:4]# 增加标题和内容之间的间距spacing = 160 # 标题和内容之间的间距# 计算总的文本高度total_text_height = title_height + content_height + spacing# 计算文本的中心位置x = (width - max(title_width, content_width)) // 2y = (height - total_text_height) // 2# 计算标题的 X 轴坐标title_x = (width - title_width) // 2# 绘制标题draw.text((title_x, y), title, fill=title_color, font=font)# 绘制内容draw.text((x, y + title_height + spacing), content, fill=text_color, font=font)# 保存图像output_path = f'../pngs/{text}.png'if not os.path.exists(output_path):image.save(output_path)print(f"图像已保存到 {output_path}")else:print(f"图像 {output_path} 已存在,跳过生成")def generate_image_768_auto_text(text,bg="darkblue"):width, height = 768, 768if bg == "black":text_color = (0, 0, 0) # 黑色字体elif bg == "darkblue":text_color = (255, 255, 255) # 白色字体elif bg == "lightgray":text_color = (64, 64, 64) # 深灰色字体else:text_color = (0, 100, 0) # 深绿色字体font_path = r'C:\Windows\Fonts\simhei.ttf'margin = 50 # 左右留空的宽度# 加载背景图片background_image = Image.open('aaa.png').convert('RGB')draw = ImageDraw.Draw(background_image)# 自动调整字体大小以适应指定区域max_font_size = 100 # 最大字体大小min_font_size = 10 # 最小字体大小font_size = max_font_sizetext_width, text_height = width - 2 * margin + 1, height + 1 # 初始化为大于指定区域的值font = None # 初始化 font 变量while text_width > width - 2 * margin or text_height > height:font_size -= 1if font_size < min_font_size:breakfont = ImageFont.truetype(font_path, font_size)left, top, right, bottom = draw.textbbox((0, 0), text, font=font)text_width = right - lefttext_height = bottom - top# 确保 font 被正确赋值if font is None:font = ImageFont.truetype(font_path, min_font_size)left, top, right, bottom = draw.textbbox((0, 0), text, font=font)text_width = right - lefttext_height = bottom - top# 计算文本的中心位置x = (width - text_width) // 2y = (height - text_height) // 2# 绘制内容draw.text((x, y), text, fill=text_color, font=font)# 保存图像output_path = f'../pngs/{text}.png'if not os.path.exists(output_path):background_image.save(output_path)print(f"图像已保存到 {output_path}")else:print(f"图像 {output_path} 已存在,跳过生成")if __name__ == "__main__":start_time=datetime.datetime.now()# generate_image_768_auto_text("你好,我是Peony",bg="darkblue")for text in text_keywords_20241030:generate_image_768_auto_text(text)end_time=datetime.datetime.now()print(f"{len(text_keywords__)}图片用时:",end_time-start_time)