摸摸头gif图实现

This commit is contained in:
SlyAimer 2025-01-16 17:07:05 +08:00
parent 725928f340
commit 60a10dbebb
17 changed files with 114 additions and 27 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@ node_modules
.idea
chat_bot.db
cloud_music_cookies.cookie
/src/music/qrcode.png

View file

@ -23,9 +23,10 @@
- [x] 待办
- [x] 天气
- [x] 今日运势
- [ ] 今日塔罗
- [x] 点歌(网易云 需扫码登录 在 src\music 目录下)*PC端 QQ可能播放不出来 原因不明*
- [x] 图(返回图库中的图片)
- [ ] 摸摸头(待实现动图)
- [x] 摸摸头(待实现动图)
- [ ] 今日老婆
- [ ] 群老婆
@ -91,7 +92,7 @@ QQ_BOTS='
"intent": {
"c2c_group_at_messages": true
},
"use_websocket": false
"use_websocket": true
}
]
'

View file

@ -9,7 +9,7 @@ QQ_BOTS='
"intent": {
"c2c_group_at_messages": true
},
"use_websocket": false
"use_websocket": true
}
]
'

View file

@ -2,7 +2,7 @@ import os
import yaml
import random
import requests
from PIL import Image, ImageDraw
with open(os.getcwd() +'/src/image/config/image.yaml', 'r', encoding='utf-8') as f:
image = yaml.load(f.read(), Loader=yaml.FullLoader).get('image')
@ -13,6 +13,9 @@ with open(os.getcwd() +'/src/image/config/image.yaml', 'r', encoding='utf-8') as
ju_he_token = image.get('ju_he_token')
ju_he_image_list = image.get('ju_he_image_list')
app_id = image.get('app_id')
bot_account = image.get('bot_account')
qq_image_save__path = os.getcwd()+'/'+image_local_qq_image_path
"""本地图片"""
def get_image_names():
@ -28,7 +31,10 @@ def get_image_names():
"""获取QQ头像"""
def download_qq_image(member_open_id):
save_path = os.getcwd() + '/' + image_local_qq_image_path + '/' + member_open_id + '.jpg'
if not os.path.exists(qq_image_save__path):
os.makedirs(qq_image_save__path)
save_path = qq_image_save__path + '/' + member_open_id + '.jpg'
size = 640 #尺寸 40、100、140、640
url = f"https://q.qlogo.cn/qqapp/{app_id}/{member_open_id}/{size}"
response = requests.get(url) # 发送 GET 请求获取图片资源
@ -37,6 +43,21 @@ def download_qq_image(member_open_id):
file.write(response.content) # 将响应内容写入文件
return save_path
"""获取QQ头像"""
def download_qq_image_by_account(account):
if not os.path.exists(qq_image_save__path):
os.makedirs(qq_image_save__path)
if account is None:
account = bot_account
save_path = qq_image_save__path + '/' + account + '.jpg'
size = 640 # 尺寸 40、100、140、640
url = f"https://q2.qlogo.cn/headimg_dl?dst_uin={account}&spec={size}"
response = requests.get(url) # 发送 GET 请求获取图片资源
if response.status_code == 200: # 判断请求是否成功
with open(save_path, 'wb') as file: # 以二进制写入模式打开文件
file.write(response.content) # 将响应内容写入文件
return save_path
"""删除QQ头像"""
def qq_image_delete():
for root, dirs, files in os.walk(image_local_qq_image_path):
@ -60,7 +81,72 @@ def get_juhe_image_url():
return random_url
""" rua 头动图生成"""
class rua():
def __init__(self, img_file):
self.author = Image.open(img_file)
def add_png(self, png_d):
# 重置图片大小
author = self.author.resize((png_d[0], png_d[1] - png_d[2]))
# 载入素材
rua_p1 = Image.open(png_d[3])
# 创建背景模板
rua_png1 = Image.new('RGBA', (110, 110), (255, 255, 255, 255))
# 使用预定义的参数jd合成一帧的样例
rua_png1.paste(author, (110 - png_d[0], 110 - png_d[1] + png_d[2]), author)
rua_png1.paste(rua_p1, (0, 110 - png_d[1] - png_d[2]), rua_p1)
return rua_png1
def add_gif(self):
# 获取素材路径
png_dir = os.getcwd() +'/src/image/rua/'
# 获取素材列表
pst = os.listdir(png_dir)
for i in range(len(pst)):
pst[i] = png_dir + pst[i]
# 预调试好的参数,传入素材列表
jd = [[90, 90, 5, pst[0]],
[90, 87, 5, pst[2]],
[90, 84, 10, pst[3]],
[90, 81, 8, pst[4]],
[90, 78, 5, pst[5]],
[90, 75, 5, pst[6]],
[90, 72, 8, pst[7]],
[90, 74, 8, pst[8]],
[90, 77, 9, pst[9]],
[90, 80, 8, pst[1]]]
# 重置要生成的图片大小
self.author = self.author.resize((90, 90))
# 绘制模板
alpha_layer = Image.new('L', (90, 90), 0)
draw = ImageDraw.Draw(alpha_layer)
draw.ellipse((0, 0, 90, 90), fill=255)
self.author.putalpha(alpha_layer)
# gif列表
gifs = []
for i in range(len(jd)):
# 将参数传递给生成方法
# 添加到gif列表
gifs.append(self.add_png(jd[i]))
# 文件名,是否保存所有,图片列表,fps/ms
gifs[0].save(os.getcwd() + '/' + image_local_qq_image_path + '/rua.gif', "GIF", save_all=True, append_images=gifs, duration=35, loop=0)
self.author.close()
return os.getcwd() + '/' + image_local_qq_image_path + '/rua.gif'
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()

BIN
src/image/rua/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
src/image/rua/10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5 KiB

BIN
src/image/rua/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
src/image/rua/3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
src/image/rua/4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
src/image/rua/5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

BIN
src/image/rua/6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

BIN
src/image/rua/7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
src/image/rua/8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
src/image/rua/9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5 KiB

View file

@ -174,7 +174,6 @@ def netease_music_download(song_id,song_name,singer,session):
data = requests.get(url, stream=True)
file_path = os.path.join(save_path, f"{song_name}-{singer}.mp3")
file_name = os.path.basename(f"{song_name}-{singer}.mp3")
print(file_path)
with open(file_path, "wb") as f:
f.write(data.content)
if data.status_code == 200:

View file

@ -36,7 +36,6 @@ async def get_today_fortune(message: MessageEvent):
msg = Message([
MessageSegment.file_image(Path(local_image_path)),
# MessageSegment.image(url),
MessageSegment.text(content),
])
await fortune_by_sqlite.finish(msg)

View file

@ -1,8 +1,9 @@
from pathlib import Path
from nonebot.rule import to_me
from nonebot.plugin import on_command
from nonebot.adapters.qq import MessageEvent
from nonebot.adapters.qq import Message,MessageEvent,MessageSegment
from src.my_sqlite.touch_by_sqlite import touch_count, QrTouchLog, insert_touch_log, touch
from src.image.get_image import download_qq_image_by_account,qq_image_delete,rua
to = on_command("摸摸头",rule=to_me(),priority=10,block=True)
@ -10,27 +11,27 @@ to = on_command("摸摸头",rule=to_me(),priority=10,block=True)
async def handle_touch(event: MessageEvent):
member_openid = event.get_user_id()
# 判断触摸次数
if touch_count(member_openid) > 10:
await to.finish("你已经摸了太多次了,请休息一下吧!")
elif touch_count(member_openid) > 5:
num = touch_count(member_openid)
q = QrTouchLog()
q.user_id = member_openid
if num > 10 :
await to.finish("你今天已经摸了太多次了,请明天再吧!")
elif num > 5:
result = touch(1)
# 记录触摸次数
q = QrTouchLog()
q.touch_status = 0
q.touch_status = 1
q.reply_touch_content = result.reply_touch_content
q.user_id = member_openid
insert_touch_log(q)
await to.finish(result.reply_touch_content)
else:
result = touch(0)
# 记录触摸次数
q = QrTouchLog()
q.touch_status = 0
q.touch_status = 1
q.reply_touch_content = result.reply_touch_content
q.user_id = member_openid
insert_touch_log(q)
await to.finish(result.reply_touch_content)
insert_touch_log(q)
local_gif = rua(download_qq_image_by_account(None)).add_gif()
msg = Message([
MessageSegment.file_image(Path(local_gif)),
MessageSegment.text(result.reply_touch_content),
])
qq_image_delete()
await to.finish(msg)