Skip to main content

nonebot-plugin-txt2img

✨ 轻量文字转图片插件 ✨

license pypi python

简介

本插件由使用 PIL(Pillow) 库将纯文字消息转为图片,启动时会检测所需资源是否存在,若不存在会自动下载到对应位置。生成的图片以 base64 格式发送,不保存到磁盘。

安装步骤

安装 NoneBot2

完整文档可以在 这里 查看。

请在创建项目时选用合适的适配器,并且按照文档完成最小实例的创建。

安装 nonebot-plugin-txt2img

使用 nb-cli 安装(推荐)

nb plugin install nonebot-plugin-txt2img

使用 pip 安装

pip install nonebot-plugin-txt2img

需要在 bot 根目录 pyproject.toml 文件中 [tool.nonebot] 部分添加:

plugins = ["nonebot_plugin_txt2img"]

Driver 设置

自动下载资源文件需要参考 driver 配置项,添加 ForwardDriver 支持,在对应 env 文件(如 .env .env.prod)中,如:

DRIVER=~fastapi+~httpx

代理设置

在对应 env 文件(如 .env .env.prod)中,可以修改下载资源时使用的 GitHub 代理:

GITHUB_PROXY="https://ghproxy.com"

指令说明

指令匹配方式添加了 to_me() 规则,在群聊中使用时需要在命令首部或尾部添加 @机器人 或 机器人昵称。

使用指令:txt2img

发送指令后根据提示输入标题、内容与字体大小,即可完成图片生成。

  • 标题:以 1.5 倍字体大小排版在首行居中位置。
  • 内容:以 1 倍字体大小左对齐排版。
  • 字体大小:位于 20~120 之间的数字。

若内容不满一行,或每行都是较短的内容,会根据内容文本宽度调节图片宽度。

跨插件使用

如需在其他插件中使用文本转图片功能,可以从本插件导入。

导入方式:

from nonebot import require
require("nonebot_plugin_txt2img")
from nonebot_plugin_txt2img import Txt2Img

基本使用方式(以 OneBot V11 为例):

from nonebot.adapters.onebot.v11 import MessageSegment

# 标题设置为 '' 或 ' ' 可以去除标题行
title = '标题'
text = '正文内容'
font_size = 32

txt2img = Txt2Img()

# 设置字体大小
txt2img.set_font_size(font_size)

# # 同时设置内容与标题字体大小
# title_font_size = 48
# txt2img.set_font_size(font_size, title_font_size)

# # 设置固定宽度
# # 设置后不会在内容较窄时自动调整宽度
# width = 1080
# txt2img.set_width(1080)

# # 绘制 PIL.Image.Image 图片
# pic = txt2img.draw_img(title, text)

# 绘制 ByteIO 图片并发送
pic = txt2img.draw(title, text)
msg = MessageSegment.image(pic)

使用模板:

插件内置 ["default", "simple"] 两个模板,分别是小米便笺以及黑底白字简单风格。默认模板是 "default",可使用以下代码修改使用的模板:

...
# 使用简约模板
pic = txt2img.draw(title, text, "simple")
msg = MessageSegment.image(pic)

也可以传入一个 Template 对象以实现自定义模板,具体字段请参考代码内的标注,示例如下:

from nonebot_plugin_txt2img import Template

template = Template(...)
...
# 使用自定义模板
pic = txt2img.draw(title, text, template)
msg = MessageSegment.image(pic)

项目致谢

本项目基于以下项目或服务实现,排名不分先后。

Metadata

Release files for nonebot-plugin-txt2img 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nonebot-plugin-txt2img 0.4.1
File Size Uploaded Provenance Actions
nonebot_plugin_txt2img-0.4.1.tar.gz 9.0 kB Without provenance Details for nonebot_plugin_txt2img-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nonebot-plugin-txt2img 0.4.1
File Interpreter ABI Platform Actions
nonebot_plugin_txt2img-0.4.1-py3-none-any.whl Python 3 none any Details for nonebot_plugin_txt2img-0.4.1-py3-none-any.whl

Total release size: 19.8 kB

Release files / nonebot_plugin_txt2img-0.4.1.tar.gz

Download URL nonebot_plugin_txt2img-0.4.1.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b3d6535548888af39307d6de186087cf511f8d9cd16e046696544cca69f0bb2d
BLAKE2b-256 checksum
How to use checksums
ab888811ffaf1b66f7c85c18a4e61d70c8865a714a8730844db852a13cf9a71a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.10 Windows/10

Release files / nonebot_plugin_txt2img-0.4.1-py3-none-any.whl

Download URL nonebot_plugin_txt2img-0.4.1-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cde183ade8aa556e554207cb8324471e4e2e626257184d667bda7a342fe5d52b
BLAKE2b-256 checksum
How to use checksums
736a2fc5c124939dc9e3df199cc99e44fb6a6ab41e25a29167e6afe8bbebb763
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.10 Windows/10

Security for nonebot-plugin-txt2img 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page