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


相关文章
|
11天前
|
人工智能 自然语言处理 IDE
6 款 AI 工具,助力写出更优质代码
6 款 AI 工具,助力写出更优质代码
124 3
6 款 AI 工具,助力写出更优质代码
|
8天前
|
人工智能 Linux iOS开发
exo:22.1K Star!一个能让任何人利用日常设备构建AI集群的强大工具,组成一个虚拟GPU在多台设备上并行运行模型
exo 是一款由 exo labs 维护的开源项目,能够让你利用家中的日常设备(如 iPhone、iPad、Android、Mac 和 Linux)构建强大的 AI 集群,支持多种大模型和分布式推理。
239 100
|
4天前
|
人工智能 自然语言处理 数据可视化
Data Formulator:微软开源的数据可视化 AI 工具,通过自然语言交互快速创建复杂的数据图表
Data Formulator 是微软研究院推出的开源 AI 数据可视化工具,结合图形化界面和自然语言输入,帮助用户快速创建复杂的可视化图表。
162 9
Data Formulator:微软开源的数据可视化 AI 工具,通过自然语言交互快速创建复杂的数据图表
|
12天前
|
人工智能 自然语言处理 API
OpenDeepResearcher:开源 AI 研究工具,自动完成搜索、评估、提取和生成报告
OpenDeepResearcher 是一款开源 AI 研究工具,支持异步处理、去重功能和 LLM 驱动的决策,帮助用户高效完成复杂的信息查询和分析任务。
153 18
OpenDeepResearcher:开源 AI 研究工具,自动完成搜索、评估、提取和生成报告
|
22天前
|
人工智能 自然语言处理 语音技术
FilmAgent:多智能体共同协作制作电影,哈工大联合清华推出 AI 驱动的自动化电影制作工具
FilmAgent 是由哈工大与清华联合推出的AI电影自动化制作工具,通过多智能体协作实现从剧本生成到虚拟拍摄的全流程自动化。
239 10
FilmAgent:多智能体共同协作制作电影,哈工大联合清华推出 AI 驱动的自动化电影制作工具
|
5天前
|
人工智能 JSON 搜索推荐
猫步简历 - 开源免费AI简历生成器 | 一键导出PDF/JSON
猫步简历是一款免费开源的AI简历生成器,帮助用户轻松创建独特、专业的简历。支持导出超高清PDF、图片、JSON等多种格式,并提供AI智能创作、润色和多语种切换等功能。拥有海量模板、高度定制化模块及完善的后台管理系统,助力求职者脱颖而出。官网:https://maobucv.com,GitHub开源地址:https://github.com/Hacker233/resume-design。
75 10
|
1月前
|
人工智能 资源调度 JavaScript
PPTAgent:中科院开源AI工具,自动将文档转化为高质量PPT
PPTAgent 是中科院推出的自动生成演示文稿框架,基于两阶段编辑方法,支持智能分析、大纲生成、幻灯片生成与评估,适用于教育、企业培训等多种场景。
430 18
PPTAgent:中科院开源AI工具,自动将文档转化为高质量PPT
|
9天前
|
人工智能 安全 开发工具
Repomix:8.1K Star!轻松将整个代码库打包为AI友好格式的开源工具,使代码库更易于AI理解
Repomix 是一款强大的工具,能够将整个代码库打包成AI友好的单个文件,支持多种输出格式和安全检查。
68 9
|
28天前
|
机器学习/深度学习 人工智能 文字识别
Zerox:AI驱动的万能OCR工具,精准识别复杂布局并输出Markdown格式,支持PDF、DOCX、图片等多种文件格式
Zerox 是一款开源的本地化高精度OCR工具,基于GPT-4o-mini模型,支持PDF、DOCX、图片等多种格式文件,能够零样本识别复杂布局文档,输出Markdown格式结果。
113 4
Zerox:AI驱动的万能OCR工具,精准识别复杂布局并输出Markdown格式,支持PDF、DOCX、图片等多种文件格式
|
8天前
|
人工智能 搜索推荐 数据管理
[转载] 太神奇了!钉钉低代码×DeepSeek =5分钟手搓出学生个性化习题AI生成器
钉钉低代码宜搭通过拖拉拽让人人都可以成为开发者。未来,在通用人工智能时代,开发更是易于反掌。为了探索如何将 DeepSeek 等最先进的AI大模型融合到自己组织的工作流中,职校覃老师就尝试用钉钉低代码平台手搓一个学生个性化习题生成器。
593 2

热门文章

最新文章