图文合成方式修改

This commit is contained in:
SlyAimer 2025-02-06 22:42:34 +08:00
parent ff560a392e
commit 94df2c7653
11 changed files with 121 additions and 61 deletions

1
.gitignore vendored
View file

@ -18,3 +18,4 @@ bili.cookie
.mp4
/bili.cookie
/src/configs/api_config.py
/src/resources/temp

View file

@ -3,10 +3,9 @@ import random
import requests
from PIL import Image, ImageDraw,ImageFont
from src.configs.path_config import image_local_path,image_local_qq_image_path,rua_png
from src.configs.path_config import image_local_path,image_local_qq_image_path,rua_png,temp_path
from src.configs.api_config import smms_token,smms_image_upload_history,ju_he_token,ju_he_image_list,app_id,bot_account
"""本地图片"""
def get_image_names():
image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp'] # 定义常见的图片文件扩展名
@ -134,33 +133,79 @@ class rua():
"""
图文合成
"""
def add_text_to_image(image_path, text, output_path, font_size):
async def add_text_to_image(image_path, output_path,content,font_path, font_size, text_color, position):
"""
给图片添加文字
:param image_path: 输入图片的路径
:param output_path: 合成后的图片名称
:param content: 要添加的文字内容
:param font_path: 字体文件路径
:param font_size: 文字的字体大小
:param text_color: 文字颜色 (255, 0, 0) "#FF0000" "red"
:param position: 文字位置可选值"left", "right", "center", "top", "bottom", "top left corner", "top right corner", "bottom left corner", "bottom right corner"
:return:
"""
# 打开图片
image = Image.open(image_path)
# 创建一个可用于绘制的对象
draw = ImageDraw.Draw(image)
# 设置字体和字体大小
font = ImageFont.truetype("arial.ttf", font_size) # 这里使用 Arial 字体,你可以替换为其他字体文件路径
# 设置文本的位置和颜色
position = (50, 50)
text_color = (255, 0, 0) # 红色
font = ImageFont.truetype(font_path, font_size)
wrapped_text,current_width = "",0
# 遍历文本中的每个字符
for char in content:
# 获取字符的宽度
char_width, _ = draw.textbbox((0, 0), char, font=font)[2:]
# 如果当前行的宽度加上字符宽度超过图片指定宽度,则换行
if current_width + char_width > image.width // 2: # 这里是图片的一半
wrapped_text += "\n"
current_width = 0
# 将字符添加到当前行
wrapped_text += char
# 更新当前行的宽度
current_width += char_width
# 获取换行后文本的宽度和高度
text_width, text_height = draw.textbbox((0, 0), wrapped_text, font=font)[2:]
# 根据位置参数计算文本的位置
if position == "left":
position = (0, (image.height - text_height) // 2)
elif position == "right":
position = (image.width - text_width, (image.height - text_height) // 2)
elif position == "center":
position = ((image.width - text_width) // 2, (image.height - text_height) // 2)
elif position == "top":
position = ((image.width - text_width) // 2, 0)
elif position == "bottom":
position = ((image.width - text_width) // 2, image.height - text_height)
elif position == "top left corner":
position = (0, 0)
elif position == "top right corner":
position = (image.width - text_width, 0)
elif position == "bottom left corner":
position = (0, image.height - text_height)
elif position == "bottom right corner":
position = (image.width - text_width, image.height - text_height)
# 在图片上绘制文本
draw.text(position, text, font=font, fill=text_color)
draw.multiline_text(position, wrapped_text, font=font, fill=text_color, align="center")
# 保存合成后的图片
image.save(output_path)
print(f"合成后的图片已保存到 {output_path}")
# 关闭图片
# image.close()
if __name__ == '__main__':
print(get_smms_image_url())
print(get_juhe_image_url())
print(get_image_names())
file_path = '8A91A2F3BE5B5AF3FEC97FB5AA6D9B38.jpg'
au = rua(file_path).add_gif()
image_path = "Justice.jpg"
text = "Hello, World!"
output_path = "output.jpg"
add_text_to_image(image_path, text, output_path, font_size=48)
# print(get_smms_image_url())
# print(get_juhe_image_url())
# print(get_image_names())
# file_path = '8A91A2F3BE5B5AF3FEC97FB5AA6D9B38.jpg'
# au = rua(file_path).add_gif()
image_path = "021.png"
content = "你是很大的哈设计开发哈卡斯萨夫卡是大华饭店不是的话覆盖过海宿管会啊傻瓜金佛上帝海水淡化你是很大的哈设计开发哈卡斯萨夫卡是大华饭店不是的话覆盖u过海宿管会啊傻瓜金佛上帝海水淡化你"
output_path = "output.png"
add_text_to_image(image_path, content, output_path, "微软雅黑.ttc",text_color = (255, 0, 0),font_size=48,position="top")

View file

@ -12,7 +12,14 @@ image_local_path= path+"/MaoYuNa"
tarots_img_path = path+'/tarot/TarotImages/'
# 摸摸头图片路径
rua_png = path+'/rua/'
# 喜报、悲报图片路径
good_bad = path+'/good_bad_news/'
# 字体路径
font_path = path+'/font/'
# 临时数据路径
temp_path = os.getcwd()+'/src/resources/temp/'
# 图片路径
@ -21,14 +28,8 @@ IMAGE_PATH = Path() / "resources" / "image"
RECORD_PATH = Path() / "resources" / "record"
# 文本路径
TEXT_PATH = Path() / "resources" / "text"
# 日志路径
LOG_PATH = Path() / "log"
# 字体路径
FONT_PATH = Path() / "resources" / "font"
# 数据路径
DATA_PATH = Path() / "data"
# 临时数据路径
TEMP_PATH = Path() / "resources" / "temp"
# 网页模板路径
TEMPLATE_PATH = Path() / "resources" / "template"

View file

@ -2,43 +2,56 @@ from pathlib import Path
from nonebot.rule import to_me
from nonebot.plugin import on_command
from nonebot.adapters.qq import Message, MessageEvent, MessageSegment, exception
import urllib.parse, requests
import urllib.parse
import requests
import time
import httpx
from src.clover_image.get_image import add_text_to_image
from src.configs.path_config import font_path,good_bad,temp_path
good_news = on_command("喜报", rule=to_me(), priority=10, block=True, aliases={"悲报"})
@good_news.handle()
async def good_news_img(message: MessageEvent):
if message.get_plaintext().startswith("/喜报"):
content = message.get_plaintext().replace("/喜报", "").strip()
url = "https://cdn.uuuix.com/api/v1/xbs/xb.php?"
else:
content = message.get_plaintext().replace("/悲报", "").strip()
url = "https://cdn.uuuix.com/api/v1/xbs/biob.php?"
params = {
'msg': content
}
await good_news.send("图片绘制中,请稍后~\n技术支持: JianDan大佬\nwww·uuuix·com")
query = urllib.parse.urlencode(params)
response = requests.get(url + query).json()
if response['code'] != 1:
await good_news.finish("请输入 /喜(悲)报+内容 哦。")
img_url = response['url']
# good_news = on_command("喜报", rule=to_me(), priority=10, block=True, aliases={"悲报"})
# @good_news.handle()
# async def good_news_img(message: MessageEvent):
# if message.get_plaintext().startswith("/喜报"):
# content = message.get_plaintext().replace("/喜报", "").strip()
# url = "https://cdn.uuuix.com/api/v1/xbs/xb.php?"
# else:
# content = message.get_plaintext().replace("/悲报", "").strip()
# url = "https://cdn.uuuix.com/api/v1/xbs/biob.php?"
#
# params = {
# 'msg': content
# }
#
# await good_news.send("图片绘制中,请稍后~\n技术支持: JianDan大佬\nwww·uuuix·com")
#
# query = urllib.parse.urlencode(params)
# response = requests.get(url + query).json()
#
# if response['code'] != 1:
# await good_news.finish("请输入 /喜(悲)报+内容 哦。")
#
# img_url = response['url']
# try:
# await good_news.finish(MessageSegment.clover_image(img_url))
# except BaseException:
# await good_news.finish("出错啦,请重试。")
try:
await good_news.finish(MessageSegment.image(img_url))
except exception.ActionFailed as e:
print("\033[32m" + str(time.strftime("%m-%d %H:%M:%S")) +
"\033[0m [" + "\033[31;1mFAILED\033[0m" + "]" +
"\033[31;1m nonebot.adapters.qq.exception.ActionFailed \033[0m" + str(e))
await good_news.finish("图片发送失败,请重试。这绝对不是咱的错,绝对不是!")
good_news = on_command("喜报", rule=to_me(), priority=10, block=True, aliases={"悲报"})
@good_news.handle()
async def function(message: MessageEvent):
value = message.get_plaintext().split(" ")
keyword,content = value[0], value[1]
if value[1] == "":
await good_news.finish("请输入 /喜(悲)报+内容 哦。")
if keyword == "/喜报":
await add_text_to_image(image_path=good_bad + "good_news.png", output_path=temp_path+"good_news.png", content=content,
font_path=font_path + "msyh.ttc", font_size=64, text_color=(255, 0, 0),
position="center")
await good_news.finish(MessageSegment.file_image(Path(temp_path+"good_news.png")))
elif keyword == "/悲报":
await add_text_to_image(image_path=good_bad + "bad_news.png", output_path=temp_path+"bad_news.png", content=content,
font_path=font_path + "msyh.ttc", font_size=64, text_color=(128, 128, 128),
position="center")
await good_news.finish(MessageSegment.file_image(Path(temp_path+"bad_news.png")))

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB