今日天气
  1. 博客/

使用 Dalle3 为博客生成背景

·497 字·1 分钟· ·
AIGC DallE3
Johny
作者
Johny
熟练的 云原生搬砖师
Table of Contents

背景
#

最近参考了[米开朗基杨](云原生实验室 (icloudnative.io)) 大佬的博客,并对本站点进行了主题更换。在迁移过程中,发现这个 Blowfish 主题可以为每一页配置一张图片。之前的主题没有这个功能,导致默认图片被使用。最近刷推荐看到了yihong0618老师的 [tg_bing_dalle](yihong0618/tg_bing_dalle: Telegram bing bot DALL-E 3 (github.com)) 项目,觉得很有意思,于是部署并试用了一下。生成出来的图片效果非常不错,正好可以用来为我的博客页面生成所需的图片。

使用
#

  1. 基于 Python SDK 与 TG 对接,对应脚本如下:

    使用时,需要将如下参数进行替换

    • token-id: https://my.telegram.org 中申请到的 ID
    • hash-id: https://my.telegram.org hash 值
    • @BingDalle668_bot : 你当前的 TG bot ID,通过 @BotFather 生成。
    from telethon import TelegramClient
    import os
    import sys
    
    arg1 = sys.argv[1]
    
    current_path = os.path.dirname(os.path.abspath(__file__))
    os.chdir(current_path)
    # 在 https://my.telegram.org 申请到的 id 和 hash 值
    client = TelegramClient("bot", "token-id", "hash-id", connection_retries=None).start()
    async def main():
        await client.send_message("@BingDalle668_bot", "/prompt_pro " + arg1)
        # Debug
        # await client.send_message("@BingDalle668_bot", "/aaa " + arg1)
        await client.send_read_acknowledge("@BingDalle668_bot")
    with client:
        client.loop.run_until_complete(main())
    

    假设,将上述脚本命名为 tg.py , 使用方法

    pip install telethon
    
    # 首先运行一次,创建脚本所需要的 bot.session
    python3.9 tg.py "生成一张 技术博客标题图片,围绕 'Slimtoolkit/Slim 与 Gitlab 集成体验',需要添加赛博朋克风格元素"
    Please enter your phone (or bot token):
    

    image-20231222162751340

    生成
    #

    这里由于我使用的 MacOS,使用 Shell 来的习惯,就用它来为 Blog 中 必要字段的提取了。

    cat run.sh
    
    #!/bin/bash
    
    IFS='
    '
    
    for i in $(for i in `ls ~/Desktop/hugo-blog/content/post/*.md`;do
            cat "$i"|grep 'description'|awk -F ': ' '{print $2}'|tr \" \' ;
    done);do
        MESSAGE="Generate a technical blog title image, centered around $i, and add cyberpunk style elements."
        python3.9 tg.py "$MESSAGE"
        sleep 10
    done
    
    # 运行
    bash run.sh
    

效果
#

酷!静静等待一场视觉盛宴吧。

2023-12-22 14.29.55

相关文章

Slimtoolkit/Slim 与 Gitlab 集成体验
·198 字·1 分钟·
SRE Slimtoolkit/slim docker
PVE AutoSnap 工具使用
·270 字·1 分钟·
Cloud PVE
Python Fro DevOps 笔记
·1661 字·4 分钟·
devops DevOps
Istio in Action 学习笔记
·40593 字·82 分钟·
Istio Istio ServiceMesh
站点开始使用 Grafana loki 统计分析
·4828 字·10 分钟·
Loki SRE DevOps Logging
Gitea Actions ActRunner 基于 Systemd 部署安装
·897 字·2 分钟·
SRE DevOps linux