DIY中秋明信片

释放双眼,带上耳机,听听看~!
本文章教你如何使用AI工具DIY中秋明信片,生成中秋祝福语,并合成明信片图片。

一、我的中秋明信片

项目地址:用【文心一言】DIY自己的中秋明信片 aistudio.baidu.com/projectdeta…

1.思路

  • sd生成中秋图片
  • 文心一言生成中秋祝福
  • 合并中秋图、祝福语

2.效果图

DIY中秋明信片

DIY中秋明信片

DIY中秋明信片

二、环境设置

  • ppdiffusers
  • aistudio
  • gradio
  • langchain
  • llama-index
  • omegaconf
  • nltk
%%capture
!pip install -r requirements.txt

三、素材生成

1.prompt翻译

from tools import translate_prompt, magic_prompt
translated = translate_prompt("广州的高楼大厦,夜晚,中秋节,月饼, 月亮,孤独的一个人")
translated
'"Guangzhou's tall buildings at night, Mid-Autumn Festival, Mooncake, Moon, lonely person"'

2.文生图

import sd

# 中秋,4k,光线追踪
prompt = translated+ ", 4K, ray tracing"
image = sd.imagine(prompt)
image
100%|██████████| 25/25 [00:06<00:00,  3.62it/s]

DIY中秋明信片

# 保存处理后的图片  
image.save('background.jpg')
image.size
(1200, 800)

3.中秋祝福语生成

import aistudio

send_messages = [{
    "role": "user",
    "content": "我的家乡在陕西,中秋节特别思念家乡的亲人,请帮我生成中秋节日祝福五言绝句"
}]

chat = aistudio.chat.create(
    messages=send_messages
)
bot_message = chat.result
bot_message
'月明故乡思,n乡愁在心间。n亲人在眼前,n祝福寄千里。'

4.过滤标点符号

# 过滤标点
import re  
  
  
# 使用正则表达式删除句号和逗号  
bot_message = re.sub(r',|。', '', bot_message)  
  
print(bot_message)
月明故乡思
乡愁在心间
亲人在眼前
祝福寄千里

四、明信片合成

from PIL import Image, ImageDraw, ImageFont


def combine_text_with_image(text, image_path, output_path,font_size, x, y):
    # Load the image
    image = Image.open(image_path)

    # Create a draw object
    draw = ImageDraw.Draw(image)

    # Load the font
    font = ImageFont.truetype("叶根友毛笔行书.ttf", font_size)  # You can change the font to any other font you have

    # Calculate the text size
    _, _, text_width, text_height = draw.textbbox((0, 0), text=text, font=font)
    # Set the position of the texts
    x = x
    y = y

    # Add the text to the image
    draw.text((x, y), text, font=font, fill="white")

    # Save the combined image
    image.save(output_path)
combine_text_with_image(bot_message, "background.jpg", "combined_image.jpg", font_size=60,x=10, y=10)
combine_text_with_image('海上生明月,天涯共此时n我在星河社区祝福大家中秋快乐!', "combined_image.jpg", "combined_image2.jpg",font_size=40, x=600, y=700)

五、gradio 部署

1.创建gradio部署文件

好多第一次使用的经常问到怎么部署?那截图来了

DIY中秋明信片

然后就会创建出事代码

#该应用创建工具共包含三个区域,顶部工具栏,左侧代码区,右侧交互效果区,其中右侧交互效果是通过左侧代码生成的,存在对照关系。
#顶部工具栏:运行、保存、新开浏览器打开、实时预览开关,针对运行和在浏览器打开选项进行重要说明:
#[运行]:交互效果并非实时更新,代码变更后,需点击运行按钮获得最新交互效果。
#[在浏览器打开]:新建页面查看交互效果。
#以下为应用创建工具的示例代码

import gradio as gr

def quickstart(name):
    return "欢迎使用BML CodeLab应用创建工具Gradio, " + name + "!!!"
demo = gr.Interface(fn=quickstart, inputs="text", outputs="text")

demo.launch()

2.撰写部署文件

# 1.安装依赖
import os
os.system("python -m pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/")

import gradio as gr
import sd
from PIL import Image, ImageDraw, ImageFont
from tools import translate_prompt, magic_prompt
import sd
import aistudio
# 过滤标点
import re  


# 2.图片生成
def generate_pic(prompt):
    # prompt翻译
    translated = translate_prompt(prompt)
    # 4k,光线追踪
    prompt = translated+ ", 4K, ray tracing"
    image = sd.imagine(prompt)
    # 保存处理后的图片  
    image.save('background.jpg')
    return 'background.jpg'

# 3.祝福语生成
def generate_5jueju(prompt):
    send_messages = [{
        "role": "user",
        "content": "我的家乡在陕西,中秋节特别思念家乡的亲人,请帮我生成中秋节日祝福五言绝句"
    }]

    chat = aistudio.chat.create(
        messages=send_messages
    )
    bot_message = chat.result
    return bot_message

# 4.合并图片及文字
def combine_text_with_image(text, image_path, output_path,font_size, x, y):
    # Load the image
    image = Image.open(image_path)

    # Create a draw object
    draw = ImageDraw.Draw(image)

    # Load the font
    font = ImageFont.truetype("叶根友毛笔行书.ttf", font_size)  # You can change the font to any other font you have

    # Calculate the text size
    _, _, text_width, text_height = draw.textbbox((0, 0), text=text, font=font)
    # Set the position of the texts
    x = x
    y = y
    # Add the text to the image
    draw.text((x, y), text, font=font, fill="white")
    # Save the combined image
    image.save(output_path)

# 5.合并文字和图片
def generate(prompt1, prompt2):
    pic=generate_pic(prompt1)
    jueju5=generate_5jueju(prompt2)   
    # 使用正则表达式删除句号和逗号  
    jueju5 = re.sub(r',|。', '', jueju5)
    combine_text_with_image(jueju5, "background.jpg", "combined_image.jpg", font_size=60,x=10, y=10)
    combine_text_with_image('海上生明月,天涯共此时n我在星河社区祝福大家中秋快乐!', "combined_image.jpg", "combined_image2.jpg",font_size=40, x=600, y=700)
    return "combined_image2.jpg"

examples =  [
            ["北京的写字间,打工人在加班,桌上阅兵,窗外明亮的月亮","我的家乡在陕西,中秋节特别思念家乡的亲人,请帮我生成中秋节日祝福五言绝句"],
            ["广州的高楼大厦,夜晚,中秋节,月饼, 月亮,孤独的一个人","我的家乡在陕西,中秋节特别思念家乡的亲人,请帮我生成中秋节日祝福五言绝句"],
            ["关中平原,一望无际的玉米地,农家小院丰盛的晚餐,阅兵,月亮","我的家乡在陕西,中秋节特别思念家乡的亲人,请帮我生成中秋节日祝福五言绝句"]
            ]

# 6.gradio部署
demo=gr.Interface(
    fn=generate,
    inputs=[gr.inputs.Textbox(lines=5, label="场景描述输入"), 
            gr.inputs.Textbox(lines=5, label="诗词描述输入"),],
    outputs=gr.Image(label="明信片输出"),
    examples=examples
)

# 7.启动项目
demo.launch()

六、项目地址

用【文心一言】DIY自己的中秋明信片 aistudio.baidu.com/projectdeta…

本网站的内容主要来自互联网上的各种资源,仅供参考和信息分享之用,不代表本网站拥有相关版权或知识产权。如您认为内容侵犯您的权益,请联系我们,我们将尽快采取行动,包括删除或更正。
AI教程

特征值特征向量及对称矩阵的相似对角化

2023-11-21 14:33:14

AI教程

Vivid Ombre Rainbow Cake Sliced to Perfection: A Delicious Treat for the Eyes and Taste Buds

2023-11-21 14:42:14

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索