From f76644585749507ef1aa9d19e14877a1290d53c8 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 3 Aug 2021 19:53:41 +0800 Subject: [PATCH] =?UTF-8?q?yvlu=20=E6=94=AF=E6=8C=81=E5=9B=9E=E5=A4=8D?= =?UTF-8?q?=E8=B4=B4=E7=BA=B8\=E5=9B=BE=E7=89=87=E7=94=9F=E6=88=90?= =?UTF-8?q?=E8=AF=AD=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- list.json | 2 +- yvlu.py | 132 ++++++++++++++++++++++++++++++++++++++++++------- yvlu/1.png | Bin 437 -> 0 bytes yvlu/2.png | Bin 197 -> 0 bytes yvlu/3.png | Bin 500 -> 0 bytes yvlu/mask1.png | Bin 0 -> 2772 bytes yvlu/p1.png | Bin 0 -> 411 bytes yvlu/p2.png | Bin 0 -> 185 bytes yvlu/p3.png | Bin 0 -> 503 bytes 9 files changed, 116 insertions(+), 18 deletions(-) delete mode 100644 yvlu/1.png delete mode 100644 yvlu/2.png delete mode 100644 yvlu/3.png create mode 100644 yvlu/mask1.png create mode 100644 yvlu/p1.png create mode 100644 yvlu/p2.png create mode 100644 yvlu/p3.png diff --git a/list.json b/list.json index 08e21bf..1966650 100644 --- a/list.json +++ b/list.json @@ -702,7 +702,7 @@ }, { "name": "yvlu", - "version": "1.0", + "version": "1.1", "section": "chat", "maintainer": "xtaodada", "size": "4.8 kb", diff --git a/yvlu.py b/yvlu.py index e294131..64a0e70 100644 --- a/yvlu.py +++ b/yvlu.py @@ -1,8 +1,12 @@ +from io import BytesIO +from math import floor from os.path import exists from os import makedirs, remove from PIL import Image, ImageFont, ImageDraw from requests import get from telethon.tl.functions.users import GetFullUserRequest +from telethon.tl.types import MessageMediaPhoto +from pagermaid import bot from pagermaid.listener import listener from pagermaid.utils import alias_command @@ -16,9 +20,9 @@ def cut(obj, sec): def yv_lu_process_image(name, text, photo, path): - if len(name) > 27: - name = name[:27] + '...' - text = cut(text, 22) + if len(name) > 16: + name = name[:16] + '...' + text = cut(text, 17) # 用户不存在头像时 if not photo: photo = Image.open(f'{path}4.png') @@ -36,8 +40,8 @@ def yv_lu_process_image(name, text, photo, path): else: photo = Image.open(f'{path}{photo}') # 读取图片 - img1, img2, img3, mask = Image.open(f'{path}1.png'), Image.open(f'{path}2.png'), \ - Image.open(f'{path}3.png'), Image.open(f'{path}mask.png') + img1, img2, img3, mask = Image.open(f'{path}p1.png'), Image.open(f'{path}p2.png'), \ + Image.open(f'{path}p3.png'), Image.open(f'{path}mask.png') size1, size2, size3 = img1.size, img2.size, img3.size photo_size = photo.size mask_size = mask.size @@ -55,7 +59,7 @@ def yv_lu_process_image(name, text, photo, path): draw = ImageDraw.Draw(img1) draw.text((60, 10), name, (255, 110, 164), font(path, 18)) for i in range(len(text)): - temp = Image.open(f'{path}2.png') + temp = Image.open(f'{path}p2.png') draw = ImageDraw.Draw(temp) draw.text((60, 0), text[i], (255, 255, 255), font(path, 18)) result.paste(temp, (0, size1[1] + size2[1] * i)) @@ -69,6 +73,43 @@ def yv_lu_process_image(name, text, photo, path): result.save(f'{path}result.png') +def yv_lu_process_sticker(name, photo, sticker, path): + # 用户不存在头像时 + if not photo: + photo = Image.open(f'{path}4.png') + # 对图片写字 + draw = ImageDraw.Draw(photo) + # 计算使用该字体占据的空间 + # 返回一个 tuple (width, height) + # 分别代表这行字占据的宽和高 + text_width = font(path, 60).getsize(name[0]) + if name[0].isalpha(): + text_coordinate = int((photo.size[0] - text_width[0]) / 2), int((photo.size[1] - text_width[1]) / 2) - 10 + else: + text_coordinate = int((photo.size[0] - text_width[0]) / 2), int((photo.size[1] - text_width[1]) / 2) + draw.text(text_coordinate, name[0], (255, 110, 164), font(path, 60)) + else: + photo = Image.open(f'{path}{photo}') + # 读取图像 + sticker = Image.open(f'{sticker}') + sticker = sticker.resize((400, 400)) + # 创建空图片 + result = Image.new('RGBA', (512, 512)) + # 处理头像 + mask = Image.open(f'{path}mask1.png') + photo_size = photo.size + mask_size = mask.size + scale = photo_size[1] / mask_size[1] + photo = photo.resize((int(photo_size[0] / scale), int(photo_size[1] / scale)), Image.LANCZOS) + mask1 = Image.new('RGBA', mask_size) + mask1.paste(photo, mask=mask) + # 粘贴图像 + result.paste(mask1, (10, 350)) + result.paste(sticker, (130, 50)) + # 保存图像 + result.save(f'{path}result.png') + + @listener(is_plugin=True, outgoing=True, command=alias_command("yvlu"), description="将回复的消息转换成语录") async def yv_lu(context): @@ -78,24 +119,34 @@ async def yv_lu(context): if not exists('plugins/yvlu/'): makedirs('plugins/yvlu/') await context.edit('处理中。。。') + # 下载资源文件 for num in range(1, 5): - if not exists('plugins/yvlu/' + str(num) + '.png'): - re = get('https://raw.githubusercontent.com/Xtao-Labs/PagerMaid_Plugins/master/yvlu/' + str(num) + '.png') - with open('plugins/yvlu/' + str(num) + '.png', 'wb') as bg: + if not exists('plugins/yvlu/p' + str(num) + '.png'): + re = get('https://raw.githubusercontent.com/Xtao-Labs/PagerMaid_Plugins/master/yvlu/p' + str(num) + '.png') + with open('plugins/yvlu/p' + str(num) + '.png', 'wb') as bg: bg.write(re.content) if not exists('plugins/yvlu/mask.png'): re = get('https://raw.githubusercontent.com/Xtao-Labs/PagerMaid_Plugins/master/yvlu/mask.png') with open('plugins/yvlu/mask.png', 'wb') as bg: bg.write(re.content) + if not exists('plugins/yvlu/mask1.png'): + re = get('https://raw.githubusercontent.com/Xtao-Labs/PagerMaid_Plugins/master/yvlu/mask1.png') + with open('plugins/yvlu/mask1.png', 'wb') as bg: + bg.write(re.content) if not exists('plugins/yvlu/ZhuZiAWan-2.ttc'): await context.edit('下载字体中。。。') re = get('https://raw.githubusercontent.com/Xtao-Labs/Telegram_PaimonBot/master/assets/fonts/ZhuZiAWan-2.ttc') with open('plugins/yvlu/ZhuZiAWan-2.ttc', 'wb') as bg: bg.write(re.content) + # 获取回复信息 reply_message = await context.get_reply_message() + if not reply_message: + await context.edit('你需要回复一条消息。') + return user_id = reply_message.sender_id target_user = await context.client(GetFullUserRequest(user_id)) - await context.client.download_profile_photo( + # 下载头像 + await bot.download_profile_photo( target_user.user.id, "plugins/yvlu/" + str(target_user.user.id) + ".jpg", download_big=True @@ -103,18 +154,65 @@ async def yv_lu(context): name = target_user.user.first_name if target_user.user.last_name: name += f' {target_user.user.last_name}' + # 判断是否为贴纸 + file_name = None + if reply_message and reply_message.media: + if isinstance(reply_message.media, MessageMediaPhoto): + file_name = 'plugins/yvlu/sticker.jpg' + await bot.download_media(reply_message.photo, file_name) + elif "image" in reply_message.media.document.mime_type.split('/'): + file_name = 'plugins/yvlu/sticker.jpg' + await bot.download_file(reply_message.media.document, file_name) + else: + await context.edit('不支持的文件类型。') + return if exists("plugins/yvlu/" + str(target_user.user.id) + ".jpg"): - yv_lu_process_image(name, reply_message.text, f"{target_user.user.id}.jpg", 'plugins/yvlu/') + if file_name: + yv_lu_process_sticker(name, f"{target_user.user.id}.jpg", file_name, 'plugins/yvlu/') + remove(file_name) + else: + yv_lu_process_image(name, reply_message.message, f"{target_user.user.id}.jpg", 'plugins/yvlu/') remove("plugins/yvlu/" + str(target_user.user.id) + ".jpg") else: - yv_lu_process_image(name, reply_message.text, None, 'plugins/yvlu/') - force_document = True - if context.arguments: - force_document = False + if file_name: + yv_lu_process_sticker(name, None, file_name, 'plugins/yvlu/') + remove(file_name) + else: + yv_lu_process_image(name, reply_message.message, None, 'plugins/yvlu/') + # 转换为贴纸 + image = await resize_image('plugins/yvlu/result.png') + file = BytesIO() + file.name = "sticker.webp" + image.save(file, "WEBP") + file.seek(0) await context.client.send_file( context.chat_id, - 'plugins/yvlu/result.png', - force_document=force_document, + file, + force_document=False, reply_to=context.message.reply_to_msg_id ) await context.delete() + + +async def resize_image(photo): + image = Image.open(photo) + maxsize = (512, 512) + if (image.width and image.height) < 512: + size1 = image.width + size2 = image.height + if image.width > image.height: + scale = 512 / size1 + size1new = 512 + size2new = size2 * scale + else: + scale = 512 / size2 + size1new = size1 * scale + size2new = 512 + size1new = floor(size1new) + size2new = floor(size2new) + size_new = (size1new, size2new) + image = image.resize(size_new) + else: + image.thumbnail(maxsize) + + return image diff --git a/yvlu/1.png b/yvlu/1.png deleted file mode 100644 index 1eb6e215ef4fdb2c7d4453c1a4daf6f4b6472209..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 437 zcmeAS@N?(olHy`uVBq!ia0y~yU;;83# zXnm@IZT7(jY=@^DRPnj~fImX;u$ZfXW#~uq3{!5g!<#fBc(}5}e45(~Un*EAHM6Dr zY?ipPY>o7vbm}!}UKjC?K_%-~rMqqYie+YT`Ao(&QH2>PZ@%9<*z=U% z;n1n;XK(*b+E}q~|L(g!sb^x?4?I5b!0Ge4tuqd2FnpL-^!ne!Wr5bVeovqMKCFl0{{T{XEXo+0F)uI4ZxOrw_brX000n4Yy%LEC3-pk2LK>HVjF;i)7XFl zX#fBqlE?;NWpk$^`Se_=Kq>$Lj{_8i1i`=R)lAs-18D002pvn;I|H=4XynO4+PH4q3T; zq1x4L*YeHnot4cW03e9h*!&k~r#4heS;V7&t6KX!3Zwx5Ku(Ms{){vK!EXEhy3-N< zc#p1&*ml)n{1-s6A`nfOiuw5}MKBUi*Mho~Z}=l|v*fi~sE}BPQl5{$4FB=jk8}Oo zb)XU+1)RlbXUp5yR|HSaOTKQQX#Cq4N9?wAEi%;ws(39g}e qFy z`BEF%6~>QH3oU@yWNh%@!PuFJ?O8I({FA51tK>CPm8vA3@gyD_JS=80o7jYqT2iW2 zzr!440g`AnLb_kotwy5N%lYnE?zvm+@BjRpM1+`#3^gff5y^_kRFg6(BI6>Gp(Huq ze)hO#fU1c2B62Pw1zT2ulO^CAvC0xzP+ur)hniq(z> zMk8z)<-Fkh;fCK2qj?rVjV_5u(G>k0)cz9@$&1L5IjWZI@ZAa8azv>{MOPGrj3Ves z1Rn@_%m{oEGID&cL@5aYnF)$ViC=TS7{5t!Z=X`8oI^~}98Vx+TSPulHYl4SvKxV% z>gX0BF$5S0{Go`B4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-LLoWIUKQ=@_&)-maYI~(TtE9 z&G=s%cG((C1xm&S5!HLzAA46tu-BzVim279@h3xOXBiiHU5^@m<0B^O z9kSf*WT|oCf(6p^ON75VNi^k>k2j)->;kR$85!;%(<^&rZ%Q6&sJ3i2DH);6xQU`y zqKw==K*qn3;f+m*R9|rGiQN02%)fk^j&d&$n}-b_pq)Evtvjf;Fl!(*w4=Yz(q757aGid@F=;wQxA>%5MhKPrWvp<;ES#?`@HhAk z%~7?z|Ed7qDH$Eq0wve|5x zwcDaw!ZM4G1wrwv# aME(z>>ISh-$G+4600001s$;8XIGLcBnxtCKygD+HM<~@WV8+y!NfyG>izZ3jndxV~Al=}O zYWaG*`7iIjd%5#h?frY-eSu~}KwYTX+1G2XzmC5y9c``5emp&R>F%O+QER_$DK%)Y zzjpg}@M&J&X=>_RzqPqHA3Yy`K4q5uv;VFV$DU6M*Y65X_MGIiy;h^)+bwD7lks!% z^4F>Bwk_0tTc40vSLrLlu+PW*+>v9v#jCYn`hC{h%zSw9Zg+L#MGOK@-fQ>k-^}^; z>qo)4Y^CqsW|H*UUy$~A%QzCEaktG3V{ojl72#1XvEX zU%9)3x#~dZEV&mAc_%nOwz05GOXI3|S$Z(;*^y~qx1B%p_l!NmpNo(GR^{!Vw?B%d tK{=5@p@p$QfQ`XPf`Nl1&ia`@8HB&FYD8x*>jJuk!PC{xWt~$(69AXII$;0+ literal 0 HcmV?d00001 diff --git a/yvlu/p3.png b/yvlu/p3.png new file mode 100644 index 0000000000000000000000000000000000000000..09ed881b2ba3c133e42c76e3c13ff4c6311738ff GIT binary patch literal 503 zcmVwBMEX9BMD4sNomMJ z(1NJFp&<&QvKm4oLnz9?L(liK?`OI3{oe0)+?RU?Cu0QU^Z8$k)btF78DrxV_5c7- z1KZhje4^ZC1xyM60KP&n006)@WP@QNwA;c#82|uvhiot;QkemKUjP88JhH)XaFQD0 zpbP+jx}%`Dp{4b$zU=dhnuAgR0Jx-7Dwtd@f6a<0j$|+dLJ=>HkpKWn(`mKLsMlTH ziRclzqa^pVZ{hy&NzXwk007+5X*G$Fe%HR3`bY)C@Qi;xb$;24w*>%z(o9ApQ#Ulm zSLS99#k7gUQBp7xvn&UKi`O@|%?JemfYPjH^M%!7+VM?Iu8D0EiCI1>S*6w^?MV=#bmPbAM>RDl_Z6|<5OYO(VeSV0UU`XGlIuy zEA&FjzC@Fm!kg`w)AmNCPaoG}uPT9B4|r0Ic~n#MDE&EWm;J}A#oPx728sNKu!tqo tg2!n`AaaD-1CA$@g7vRCOA-u>v3K`Ye<>2Ae98a-002ovPDHLkV1ng3