[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())


相关文章
|
2月前
|
人工智能 搜索推荐 算法
用AI提示词搞定基金定投:技术人的理财工具实践
本文将AI提示词工程应用于基金定投,为技术人打造一套系统化、可执行的理财方案。通过结构化指令,AI可生成个性化定投策略,覆盖目标设定、资产配置、风险控制与动态调整,帮助用户降低决策门槛,规避情绪干扰,实现科学理财。
580 13
|
2月前
|
SQL 人工智能 机器人
AI Agent新范式:FastGPT+MCP协议实现工具增强型智能体构建
FastGPT 与 MCP 协议结合,打造工具增强型智能体新范式。MCP 如同 AI 领域的“USB-C 接口”,实现数据与工具的标准化接入。FastGPT 可调用 MCP 工具集,动态执行复杂任务,亦可作为 MCP 服务器共享能力。二者融合推动 AI 应用向协作式、高复用、易集成的下一代智能体演进。
325 0
|
2月前
|
机器学习/深度学习 人工智能 算法
AI可以做电商主图了:技术原理,AI电商图生成工具对比及技术解析
双十一临近,电商主图需求激增。AI技术凭借多图融合、扩散模型等,实现高效智能设计,30秒生成高质量主图,远超传统PS效率。支持风格迁移、背景替换、文案生成,助力商家快速打造吸睛商品图,提升转化率。
732 0
|
2月前
|
人工智能 搜索推荐 数据可视化
当AI学会“使用工具”:智能体(Agent)如何重塑人机交互
当AI学会“使用工具”:智能体(Agent)如何重塑人机交互
334 115
|
2月前
|
人工智能 安全 搜索推荐
AI的下一个前沿:从静态工具到动态代理
AI的下一个前沿:从静态工具到动态代理
229 113
|
2月前
|
人工智能 自然语言处理 安全
从工具到伙伴:AI代理(Agent)是下一场革命
从工具到伙伴:AI代理(Agent)是下一场革命
266 117
|
2月前
|
人工智能 缓存 运维
【智造】AI应用实战:6个agent搞定复杂指令和工具膨胀
本文介绍联调造数场景下的AI应用演进:从单Agent模式到多Agent协同的架构升级。针对复杂指令执行不准、响应慢等问题,通过意图识别、工具引擎、推理执行等多Agent分工协作,结合工程化手段提升准确性与效率,并分享了关键设计思路与实践心得。
451 20
【智造】AI应用实战:6个agent搞定复杂指令和工具膨胀
|
2月前
|
机器学习/深度学习 人工智能 自然语言处理
双 11 电商效率突围:10 款 AI 工具的技术落地与实践指南
2025年双11促销长达38天,电商迎来AI重构关键期。本文剖析10款主流AI工具技术原理,涵盖设计、文案、投放、客服等场景,揭示计算机视觉、自然语言处理等技术如何驱动电商智能化升级,助力企业高效应对大促挑战。
373 1
|
2月前
|
人工智能 搜索推荐 UED
一个牛逼的国产AI自动化工具,开源了 !
AiPy是国产开源AI工具,结合大语言模型与Python,支持本地部署。用户只需用自然语言描述需求,即可自动生成并执行代码,轻松实现数据分析、清洗、可视化等任务,零基础也能玩转编程,被誉为程序员的智能助手。
|
2月前
|
机器学习/深度学习 人工智能
AI重塑电商拍摄:技术驱动的商业变革——5款AI模特图生成工具技术分析
AI技术正重塑电商拍摄:低成本、高效率生成逼真模特图,支持批量换装、换背景,助力商家快速上架、灵活试错。燕雀光年、Kaiber等工具实测好用,未来AI与实拍将互补共存。
260 0