[AI创造营]Wechaty实用小工具---九宫图爱心生成器~

简介: [AI创造营]Wechaty实用小工具---九宫图爱心生成器~

背景

跟随抖音风~九宫图截图、截图、截图!手都截酸掉了吧!来吧~不需要你操作,我来!你只需要发送图片,结果自动生成


效果图

308c9fd10becbf15e3ef2d970decf79d.png

话不多说,肝着~


视频观赏

视频地址:https://www.bilibili.com/video/BV1eo4y1U7Pd/


wechaty的安装不再讲解,请看上期:

[AI创造营]Wechaty实用小工具—证件照助手

https://aistudio.baidu.com/aistudio/projectdetail/2253862


第一步 环境准备

当你成功的运行起来docker,并通过二维码登录之后,Wechaty的环境就已经成功搭建了。

下面只需要导入部分库就可。

import os
import asyncio
from wechaty import Wechaty, Message, FileBox
from PIL import Image
import time
os.environ['WECHATY_PUPPET_SERVICE_TOKEN'] = '换成你的token'


第二步 九宫图生成函数编写

左侧记得新建image和result文件夹

def makeJiuPic(img_path):
    # 拿到图片路径,先把原图像重置大小
    img = Image.open(img_path)
    img = img.resize((304, 304), Image.ANTIALIAS)
    print("大小更改成功304")
    # 这张图也是最终的中心图,先进行存储
    img.save("result/5.png")
    # 再次拿到开始缩放为每张图的小图人物图100*100
    img = img.resize((100, 100), Image.ANTIALIAS)
    img = img.convert("RGBA")
    print("大小更改成功100")
    # 底稿的白底304x304
    ground = Image.open("white304x304.png")
    ground = ground.convert("RGBA")
    # 底稿的粉红色小图100*100
    pink = Image.open("pink100x100.png")
    pink = pink.convert("RGBA")
    print("图片读取成功")
    # 准备好坐标值
    zuobiao = [(0,0),(102,0),(204,0),
               (0,102),(102,102),(204,102),
               (0,204),(102,204),(204,204)]
    print(zuobiao)
    # 初始化底稿的白底304x304
    ground = Image.open("white304x304.png")
    ground = ground.convert("RGBA")
    # 生成第一张爱心图
    for i in range(9):
        if(i==0 or i==1 or i==2 or i==3 or i==4 or i==6):
            ground.paste(pink, zuobiao[i], mask=pink)
        else:
            ground.paste(img,zuobiao[i], mask=img)
    # 保存图片
    ground.save("result/1.png")
    # 初始化底稿的白底304x304
    ground = Image.open("white304x304.png")
    ground = ground.convert("RGBA")
    # 生成第二张爱心图
    for i in range(9):
        if (i == 0 or i == 1 or i == 2 or i == 4 ):
            ground.paste(pink, zuobiao[i], mask=pink)
        else:
            ground.paste(img, zuobiao[i], mask=img)
    # 保存图片
    ground.save("result/2.png")
    # 初始化底稿的白底304x304
    ground = Image.open("white304x304.png")
    ground = ground.convert("RGBA")
    # 生成第三张爱心图
    for i in range(9):
        if (i == 0 or i == 1 or i == 2 or i == 4 or i == 5 or i == 8):
            ground.paste(pink, zuobiao[i], mask=pink)
        else:
            ground.paste(img, zuobiao[i], mask=img)
    # 保存图片
    ground.save("result/3.png")
    # 初始化底稿的白底304x304
    ground = Image.open("white304x304.png")
    ground = ground.convert("RGBA")
    # 生成第四张爱心图
    for i in range(9):
        if (i == 6):
            ground.paste(pink, zuobiao[i], mask=pink)
        else:
            ground.paste(img, zuobiao[i], mask=img)
    # 保存图片
    ground.save("result/4.png")
    # 初始化底稿的白底304x304
    ground = Image.open("white304x304.png")
    ground = ground.convert("RGBA")
    # 生成第六张爱心图
    for i in range(9):
        if (i == 8):
            ground.paste(pink, zuobiao[i], mask=pink)
        else:
            ground.paste(img, zuobiao[i], mask=img)
    # 保存图片
    ground.save("result/6.png")
    # 初始化底稿的白底304x304
    ground = Image.open("white304x304.png")
    ground = ground.convert("RGBA")
    # 生成第七张爱心图
    for i in range(9):
        if (i == 0 or i == 1 or i == 3 or i == 4 or i == 5 or i == 6 or i == 7 or i == 8):
            ground.paste(pink, zuobiao[i], mask=pink)
        else:
            ground.paste(img, zuobiao[i], mask=img)
    # 保存图片
    ground.save("result/7.png")
    # 初始化底稿的白底304x304
    ground = Image.open("white304x304.png")
    ground = ground.convert("RGBA")
    # 生成第八张爱心图
    for i in range(9):
        if (i == 6 or i == 8):
            ground.paste(pink, zuobiao[i], mask=pink)
        else:
            ground.paste(img, zuobiao[i], mask=img)
    # 保存图片
    ground.save("result/8.png")
    # 初始化底稿的白底304x304
    ground = Image.open("white304x304.png")
    ground = ground.convert("RGBA")
    # 生成第九张爱心图
    for i in range(9):
        if (i == 1 or i == 2 or i == 3 or i == 4 or i == 5 or i == 6 or i == 7 or i == 8):
            ground.paste(pink, zuobiao[i], mask=pink)
        else:
            ground.paste(img, zuobiao[i], mask=img)
    # 保存图片
    ground.save("result/9.png")


第三步 主函数

项目要运行在本地环境

如果要在notebook中运行,需要更改asyncio

class MyBot(Wechaty):
    async def on_message(self, msg: Message):
        talker = msg.talker()
        await talker.ready()
        if msg.type() == Message.Type.MESSAGE_TYPE_TEXT:
            if msg.text() == "ding":
                await talker.say('dong,发送一张图片获取九宫格爱心图片~')
            if msg.text() == "1":
                time.sleep(2)
                file_box = FileBox.from_file("result/1.png")
                await msg.say(file_box)
            if msg.text() == "2":
                time.sleep(2)
                file_box = FileBox.from_file("result/2.png")
                await msg.say(file_box)
            if msg.text() == "3":
                time.sleep(2)
                file_box = FileBox.from_file("result/3.png")
                await msg.say(file_box)
            if msg.text() == "4":
                time.sleep(2)
                file_box = FileBox.from_file("result/4.png")
                await msg.say(file_box)
            if msg.text() == "5":
                time.sleep(2)
                file_box = FileBox.from_file("result/5.png")
                await msg.say(file_box)
            if msg.text() == "6":
                time.sleep(2)
                file_box = FileBox.from_file("result/6.png")
                await msg.say(file_box)
            if msg.text() == "7":
                time.sleep(2)
                file_box = FileBox.from_file("result/7.png")
                await msg.say(file_box)
            if msg.text() == "8":
                time.sleep(2)
                file_box = FileBox.from_file("result/8.png")
                await msg.say(file_box)
            if msg.text() == "9":
                time.sleep(2)
                file_box = FileBox.from_file("result/9.png")
                await msg.say(file_box)
        elif msg.type() == Message.Type.MESSAGE_TYPE_IMAGE:
            await talker.say('已收到图片,生成图片ing...')
            # 将Message转换为FileBox
            img = await msg.to_file_box()
            # 获取图片名
            img_name = img.name
            # 图片保存的路径
            img_path = 'image/' + img_name
            # 将图片保存到本地文件
            await img.to_file(file_path=img_path)
            print("输入的图片路径:", img_path)
            makeJiuPic(img_path)
            # for pic in ["result/1.png", "result/2.png", "result/3.png","result/4.png", "result/5.png", "result/6.png","result/7.png", "result/8.png", "result/9.png"]:
            #     file_box = FileBox.from_file(pic)
            #     await msg.say(file_box)
            #     time.sleep(5)
            time.sleep(3)
            await talker.say("九张图片生成完毕!\n依次发送1-9获取九张图片")
async def main():
    bot = MyBot()
    await bot.start()
asyncio.run(main())


相关文章
|
10天前
|
人工智能 安全 数据可视化
2024黑帽大会最热门的九大AI网络安全工具
在2024年的黑帽大会(Black Hat 2024)上,AI驱动的网络安全工具和技术成为焦点,引领了网络安全行业新趋势。众多安全厂商和初创公司展示了他们的最新成果,利用生成式AI来管理风险、检测并对抗网络犯罪,保障企业安全。以下是大会上一些备受瞩目的AI驱动网络安全产品和服务:
|
4天前
|
人工智能 自然语言处理 程序员
下载量突破400万,百万开发者首选的 AI 编码工具通义灵码是如何炼成的?
我们非常高兴的宣布,通义灵码插件下载量突破400万啦!
下载量突破400万,百万开发者首选的 AI 编码工具通义灵码是如何炼成的?
|
7天前
|
人工智能 自然语言处理 文字识别
秒懂全文:盘点13个各具特色的AI智能阅读助手工具
在当今信息爆炸的时代,AI阅读工具正在革新我们的阅读方式,成为了提高效率、优化阅读体验的关键。这类AI阅读辅助工具,只需要上传文件或者输入链接,便可以直接以聊天对话的形式进行一键总结和智能问答,满足用户AI PDF 阅读、AI文档问答分析、AI音视频总结等多种实用需求,高效提炼信息要点精华,建立属于自己的AI知识管理和信息管理工作流。对此,根据阅读场景,精选了 13 个具有代表性、各具特点的高质量 AI 阅读助手助理。 具体如何选择,见文末总结。
31 1
秒懂全文:盘点13个各具特色的AI智能阅读助手工具
|
2天前
|
机器学习/深度学习 人工智能 算法
打造你的定制化AI工具
【8月更文挑战第18天】打造你的定制化AI工具
|
8天前
|
存储 人工智能 开发框架
一款.NET开发的AI无损放大工具
【8月更文挑战第11天】本示例介绍了一个基于.NET开发的AI无损图像放大工具架构。前端采用WPF或ASP.NET Core构建,提供直观的用户界面;后端包括图片上传、放大处理与结果存储服务。AI模型处理层负责加载预训练模型及图像预测放大。示例代码展示了图片上传与放大服务的关键逻辑,以及WPF界面设计。实际开发需关注模型选择、性能优化、用户体验、格式兼容与部署维护等方面。
|
18天前
|
人工智能 搜索推荐 语音技术
远程访问Voice Engine AI工具:无需公网IP,轻松实现高效创作
AI音频技术取得飞跃进展,OpenAI的Voice Engine能以短短15秒的音频样本生成高度真实的语音内容,广泛应用于有声书制作、视频翻译及帮助声音障碍者沟通。开源项目VALL-E-X亦能实现个性化跨语言语音合成。结合贝锐花生壳的内网穿透服务,用户可轻松实现AI工具的远程访问,无需复杂配置,极大提升了灵活性与效率。
49 1
|
21天前
|
人工智能 数据挖掘 Python
提升办公生产力工具——AI数据分析应用小浣熊
办公小浣熊广泛应用于日常数据分析、财务分析、商业分析、销售预测、市场分析等多个领域,为用户提供了强大的支持。
提升办公生产力工具——AI数据分析应用小浣熊
|
6天前
|
人工智能 自然语言处理 IDE
通义灵码 - 免费的阿里云 VS code Jetbrains AI 编码辅助工具(二)
通义灵码 - 免费的阿里云 VS code Jetbrains AI 编码辅助工具
21 0
|
6天前
|
IDE Linux 开发工具
通义灵码 - 免费的阿里云 VS code Jetbrains AI 编码辅助工具
通义灵码 - 免费的阿里云 VS code Jetbrains AI 编码辅助工具
33 0
|
9天前
|
人工智能 C# Windows
一款.NET开发的AI无损放大工具
今天大姚给大家分享一款由.NET开源(GPL-3.0 license)、基于腾讯ARC Lab提供的Real-ESRGAN模型开发的AI无损放大工具:AI-Lossless-Zoomer。