<!-- markdownlint-disable MD033 MD036 MD041-->
<p align="center">
<img src="https://github.com/mobyw/images/raw/main/Screenshots/nonebot-plugin-txt2img.png" width="400px"/>
</p>
<div align="center">
# nonebot-plugin-txt2img
_✨ 轻量文字转图片插件 ✨_
</div>
<p align="center">
<a href="https://raw.githubusercontent.com/mobyw/nonebot-plugin-txt2img/master/LICENSE">
<img src="https://img.shields.io/github/license/mobyw/nonebot-plugin-txt2img.svg" alt="license">
</a>
<a href="https://pypi.python.org/pypi/nonebot-plugin-txt2img">
<img src="https://img.shields.io/pypi/v/nonebot-plugin-txt2img.svg" alt="pypi">
</a>
<img src="https://img.shields.io/badge/python-3.9+-blue.svg" alt="python">
</p>
## 简介
本插件由使用 `PIL(Pillow)` 库将纯文字消息转为图片,启动时会检测所需资源是否存在,若不存在会自动下载到对应位置。生成的图片以 `base64` 格式发送,不保存到磁盘。
## 安装步骤
### 安装 NoneBot2
完整文档可以在 [这里](https://v2.nonebot.dev/) 查看。
请在创建项目时选用合适的适配器,并且按照文档完成最小实例的创建。
### 安装 nonebot-plugin-txt2img
#### 使用 `nb-cli` 安装(推荐)
```bash
nb plugin install nonebot-plugin-txt2img
```
#### 使用 `pip` 安装
```bash
pip install nonebot-plugin-txt2img
```
需要在 bot 根目录 `pyproject.toml` 文件中 [tool.nonebot] 部分添加:
```python
plugins = ["nonebot_plugin_txt2img"]
```
## Driver 设置
自动下载资源文件需要参考 [driver](https://nonebot.dev/docs/appendices/config#driver) 配置项,添加 `ForwardDriver` 支持,在对应 env 文件(如 `.env` `.env.prod`)中,如:
```text
DRIVER=~fastapi+~httpx
```
## 代理设置
在对应 env 文件(如 `.env` `.env.prod`)中,可以修改下载资源时使用的 GitHub 代理:
```text
GITHUB_PROXY="https://ghproxy.com"
```
## 指令说明
指令匹配方式添加了 `to_me()` 规则,在群聊中使用时需要在命令首部或尾部添加 @机器人 或 机器人昵称。
**使用指令**:txt2img
发送指令后根据提示输入标题、内容与字体大小,即可完成图片生成。
- 标题:以 `1.5` 倍字体大小排版在首行居中位置。
- 内容:以 `1` 倍字体大小左对齐排版。
- 字体大小:位于 `20~120` 之间的数字。
若内容不满一行,或每行都是较短的内容,会根据内容文本宽度调节图片宽度。
## 跨插件使用
如需在其他插件中使用文本转图片功能,可以从本插件导入。
导入方式:
```python
from nonebot import require
require("nonebot_plugin_txt2img")
from nonebot_plugin_txt2img import Txt2Img
```
基本使用方式(以 `OneBot V11` 为例):
```python
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"`,可使用以下代码修改使用的模板:
```python
...
# 使用简约模板
pic = txt2img.draw(title, text, "simple")
msg = MessageSegment.image(pic)
```
也可以传入一个 `Template` 对象以实现自定义模板,具体字段请参考代码内的标注,示例如下:
```python
from nonebot_plugin_txt2img import Template
template = Template(...)
...
# 使用自定义模板
pic = txt2img.draw(title, text, template)
msg = MessageSegment.image(pic)
```
## 项目致谢
本项目基于以下项目或服务实现,排名不分先后。
- [nonebot2](https://github.com/nonebot/nonebot2)
- [go-cqhttp](https://github.com/Mrs4s/go-cqhttp)
- [txt2img](https://github.com/taseikyo/txt2img)
Raw data
{
"_id": null,
"home_page": "https://github.com/mobyw/nonebot-plugin-txt2img",
"name": "nonebot-plugin-txt2img",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.9,<4.0",
"maintainer_email": "",
"keywords": "nonebot2,qq,plugin",
"author": "mobyw",
"author_email": "mobyw66@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/ab/88/8811ffaf1b66f7c85c18a4e61d70c8865a714a8730844db852a13cf9a71a/nonebot_plugin_txt2img-0.4.1.tar.gz",
"platform": null,
"description": "<!-- markdownlint-disable MD033 MD036 MD041-->\n<p align=\"center\">\n <img src=\"https://github.com/mobyw/images/raw/main/Screenshots/nonebot-plugin-txt2img.png\" width=\"400px\"/>\n</p>\n\n<div align=\"center\">\n\n# nonebot-plugin-txt2img\n\n_\u2728 \u8f7b\u91cf\u6587\u5b57\u8f6c\u56fe\u7247\u63d2\u4ef6 \u2728_\n\n</div>\n\n<p align=\"center\">\n <a href=\"https://raw.githubusercontent.com/mobyw/nonebot-plugin-txt2img/master/LICENSE\">\n <img src=\"https://img.shields.io/github/license/mobyw/nonebot-plugin-txt2img.svg\" alt=\"license\">\n </a>\n <a href=\"https://pypi.python.org/pypi/nonebot-plugin-txt2img\">\n <img src=\"https://img.shields.io/pypi/v/nonebot-plugin-txt2img.svg\" alt=\"pypi\">\n </a>\n <img src=\"https://img.shields.io/badge/python-3.9+-blue.svg\" alt=\"python\">\n</p>\n\n## \u7b80\u4ecb\n\n\u672c\u63d2\u4ef6\u7531\u4f7f\u7528 `PIL(Pillow)` \u5e93\u5c06\u7eaf\u6587\u5b57\u6d88\u606f\u8f6c\u4e3a\u56fe\u7247\uff0c\u542f\u52a8\u65f6\u4f1a\u68c0\u6d4b\u6240\u9700\u8d44\u6e90\u662f\u5426\u5b58\u5728\uff0c\u82e5\u4e0d\u5b58\u5728\u4f1a\u81ea\u52a8\u4e0b\u8f7d\u5230\u5bf9\u5e94\u4f4d\u7f6e\u3002\u751f\u6210\u7684\u56fe\u7247\u4ee5 `base64` \u683c\u5f0f\u53d1\u9001\uff0c\u4e0d\u4fdd\u5b58\u5230\u78c1\u76d8\u3002\n\n## \u5b89\u88c5\u6b65\u9aa4\n\n### \u5b89\u88c5 NoneBot2\n\n\u5b8c\u6574\u6587\u6863\u53ef\u4ee5\u5728 [\u8fd9\u91cc](https://v2.nonebot.dev/) \u67e5\u770b\u3002\n\n\u8bf7\u5728\u521b\u5efa\u9879\u76ee\u65f6\u9009\u7528\u5408\u9002\u7684\u9002\u914d\u5668\uff0c\u5e76\u4e14\u6309\u7167\u6587\u6863\u5b8c\u6210\u6700\u5c0f\u5b9e\u4f8b\u7684\u521b\u5efa\u3002\n\n### \u5b89\u88c5 nonebot-plugin-txt2img\n\n#### \u4f7f\u7528 `nb-cli` \u5b89\u88c5\uff08\u63a8\u8350\uff09\n\n```bash\nnb plugin install nonebot-plugin-txt2img\n```\n\n#### \u4f7f\u7528 `pip` \u5b89\u88c5\n\n```bash\npip install nonebot-plugin-txt2img\n```\n\n\u9700\u8981\u5728 bot \u6839\u76ee\u5f55 `pyproject.toml` \u6587\u4ef6\u4e2d [tool.nonebot] \u90e8\u5206\u6dfb\u52a0\uff1a\n\n```python\nplugins = [\"nonebot_plugin_txt2img\"]\n```\n\n## Driver \u8bbe\u7f6e\n\n\u81ea\u52a8\u4e0b\u8f7d\u8d44\u6e90\u6587\u4ef6\u9700\u8981\u53c2\u8003 [driver](https://nonebot.dev/docs/appendices/config#driver) \u914d\u7f6e\u9879\uff0c\u6dfb\u52a0 `ForwardDriver` \u652f\u6301\uff0c\u5728\u5bf9\u5e94 env \u6587\u4ef6\uff08\u5982 `.env` `.env.prod`\uff09\u4e2d\uff0c\u5982\uff1a\n\n```text\nDRIVER=~fastapi+~httpx\n```\n\n## \u4ee3\u7406\u8bbe\u7f6e\n\n\u5728\u5bf9\u5e94 env \u6587\u4ef6\uff08\u5982 `.env` `.env.prod`\uff09\u4e2d\uff0c\u53ef\u4ee5\u4fee\u6539\u4e0b\u8f7d\u8d44\u6e90\u65f6\u4f7f\u7528\u7684 GitHub \u4ee3\u7406\uff1a\n\n```text\nGITHUB_PROXY=\"https://ghproxy.com\"\n```\n\n## \u6307\u4ee4\u8bf4\u660e\n\n\u6307\u4ee4\u5339\u914d\u65b9\u5f0f\u6dfb\u52a0\u4e86 `to_me()` \u89c4\u5219\uff0c\u5728\u7fa4\u804a\u4e2d\u4f7f\u7528\u65f6\u9700\u8981\u5728\u547d\u4ee4\u9996\u90e8\u6216\u5c3e\u90e8\u6dfb\u52a0 @\u673a\u5668\u4eba \u6216 \u673a\u5668\u4eba\u6635\u79f0\u3002\n\n**\u4f7f\u7528\u6307\u4ee4**\uff1atxt2img\n\n\u53d1\u9001\u6307\u4ee4\u540e\u6839\u636e\u63d0\u793a\u8f93\u5165\u6807\u9898\u3001\u5185\u5bb9\u4e0e\u5b57\u4f53\u5927\u5c0f\uff0c\u5373\u53ef\u5b8c\u6210\u56fe\u7247\u751f\u6210\u3002\n\n- \u6807\u9898\uff1a\u4ee5 `1.5` \u500d\u5b57\u4f53\u5927\u5c0f\u6392\u7248\u5728\u9996\u884c\u5c45\u4e2d\u4f4d\u7f6e\u3002\n- \u5185\u5bb9\uff1a\u4ee5 `1` \u500d\u5b57\u4f53\u5927\u5c0f\u5de6\u5bf9\u9f50\u6392\u7248\u3002\n- \u5b57\u4f53\u5927\u5c0f\uff1a\u4f4d\u4e8e `20~120` \u4e4b\u95f4\u7684\u6570\u5b57\u3002\n\n\u82e5\u5185\u5bb9\u4e0d\u6ee1\u4e00\u884c\uff0c\u6216\u6bcf\u884c\u90fd\u662f\u8f83\u77ed\u7684\u5185\u5bb9\uff0c\u4f1a\u6839\u636e\u5185\u5bb9\u6587\u672c\u5bbd\u5ea6\u8c03\u8282\u56fe\u7247\u5bbd\u5ea6\u3002\n\n## \u8de8\u63d2\u4ef6\u4f7f\u7528\n\n\u5982\u9700\u5728\u5176\u4ed6\u63d2\u4ef6\u4e2d\u4f7f\u7528\u6587\u672c\u8f6c\u56fe\u7247\u529f\u80fd\uff0c\u53ef\u4ee5\u4ece\u672c\u63d2\u4ef6\u5bfc\u5165\u3002\n\n\u5bfc\u5165\u65b9\u5f0f\uff1a\n\n```python\nfrom nonebot import require\nrequire(\"nonebot_plugin_txt2img\")\nfrom nonebot_plugin_txt2img import Txt2Img\n```\n\n\u57fa\u672c\u4f7f\u7528\u65b9\u5f0f\uff08\u4ee5 `OneBot V11` \u4e3a\u4f8b\uff09\uff1a\n\n```python\nfrom nonebot.adapters.onebot.v11 import MessageSegment\n\n# \u6807\u9898\u8bbe\u7f6e\u4e3a '' \u6216 ' ' \u53ef\u4ee5\u53bb\u9664\u6807\u9898\u884c\ntitle = '\u6807\u9898'\ntext = '\u6b63\u6587\u5185\u5bb9'\nfont_size = 32\n\ntxt2img = Txt2Img()\n\n# \u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\ntxt2img.set_font_size(font_size)\n\n# # \u540c\u65f6\u8bbe\u7f6e\u5185\u5bb9\u4e0e\u6807\u9898\u5b57\u4f53\u5927\u5c0f\n# title_font_size = 48\n# txt2img.set_font_size(font_size, title_font_size)\n\n# # \u8bbe\u7f6e\u56fa\u5b9a\u5bbd\u5ea6\n# # \u8bbe\u7f6e\u540e\u4e0d\u4f1a\u5728\u5185\u5bb9\u8f83\u7a84\u65f6\u81ea\u52a8\u8c03\u6574\u5bbd\u5ea6\n# width = 1080\n# txt2img.set_width(1080)\n\n# # \u7ed8\u5236 PIL.Image.Image \u56fe\u7247\n# pic = txt2img.draw_img(title, text)\n\n# \u7ed8\u5236 ByteIO \u56fe\u7247\u5e76\u53d1\u9001\npic = txt2img.draw(title, text)\nmsg = MessageSegment.image(pic)\n```\n\n\u4f7f\u7528\u6a21\u677f\uff1a\n\n\u63d2\u4ef6\u5185\u7f6e `[\"default\", \"simple\"]` \u4e24\u4e2a\u6a21\u677f\uff0c\u5206\u522b\u662f\u5c0f\u7c73\u4fbf\u7b3a\u4ee5\u53ca\u9ed1\u5e95\u767d\u5b57\u7b80\u5355\u98ce\u683c\u3002\u9ed8\u8ba4\u6a21\u677f\u662f `\"default\"`\uff0c\u53ef\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u4fee\u6539\u4f7f\u7528\u7684\u6a21\u677f\uff1a\n\n```python\n...\n# \u4f7f\u7528\u7b80\u7ea6\u6a21\u677f\npic = txt2img.draw(title, text, \"simple\")\nmsg = MessageSegment.image(pic)\n```\n\n\u4e5f\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a `Template` \u5bf9\u8c61\u4ee5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u6a21\u677f\uff0c\u5177\u4f53\u5b57\u6bb5\u8bf7\u53c2\u8003\u4ee3\u7801\u5185\u7684\u6807\u6ce8\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a\n\n```python\nfrom nonebot_plugin_txt2img import Template\n\ntemplate = Template(...)\n...\n# \u4f7f\u7528\u81ea\u5b9a\u4e49\u6a21\u677f\npic = txt2img.draw(title, text, template)\nmsg = MessageSegment.image(pic)\n```\n\n## \u9879\u76ee\u81f4\u8c22\n\n\u672c\u9879\u76ee\u57fa\u4e8e\u4ee5\u4e0b\u9879\u76ee\u6216\u670d\u52a1\u5b9e\u73b0\uff0c\u6392\u540d\u4e0d\u5206\u5148\u540e\u3002\n\n- [nonebot2](https://github.com/nonebot/nonebot2)\n- [go-cqhttp](https://github.com/Mrs4s/go-cqhttp)\n- [txt2img](https://github.com/taseikyo/txt2img)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "\u8f7b\u91cf\u6587\u5b57\u8f6c\u56fe\u7247\u63d2\u4ef6",
"version": "0.4.1",
"project_urls": {
"Documentation": "https://github.com/mobyw/nonebot-plugin-txt2img/blob/main/README.md",
"Homepage": "https://github.com/mobyw/nonebot-plugin-txt2img",
"Repository": "https://github.com/mobyw/nonebot-plugin-txt2img"
},
"split_keywords": [
"nonebot2",
"qq",
"plugin"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "736a2fc5c124939dc9e3df199cc99e44fb6a6ab41e25a29167e6afe8bbebb763",
"md5": "d0bf364a5de59d3b0a6f47665e1706fe",
"sha256": "cde183ade8aa556e554207cb8324471e4e2e626257184d667bda7a342fe5d52b"
},
"downloads": -1,
"filename": "nonebot_plugin_txt2img-0.4.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d0bf364a5de59d3b0a6f47665e1706fe",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9,<4.0",
"size": 10831,
"upload_time": "2024-02-22T05:25:41",
"upload_time_iso_8601": "2024-02-22T05:25:41.002376Z",
"url": "https://files.pythonhosted.org/packages/73/6a/2fc5c124939dc9e3df199cc99e44fb6a6ab41e25a29167e6afe8bbebb763/nonebot_plugin_txt2img-0.4.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ab888811ffaf1b66f7c85c18a4e61d70c8865a714a8730844db852a13cf9a71a",
"md5": "7260b0af5198e15e3da7c5ff913f7df7",
"sha256": "b3d6535548888af39307d6de186087cf511f8d9cd16e046696544cca69f0bb2d"
},
"downloads": -1,
"filename": "nonebot_plugin_txt2img-0.4.1.tar.gz",
"has_sig": false,
"md5_digest": "7260b0af5198e15e3da7c5ff913f7df7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9,<4.0",
"size": 9013,
"upload_time": "2024-02-22T05:25:45",
"upload_time_iso_8601": "2024-02-22T05:25:45.635799Z",
"url": "https://files.pythonhosted.org/packages/ab/88/8811ffaf1b66f7c85c18a4e61d70c8865a714a8730844db852a13cf9a71a/nonebot_plugin_txt2img-0.4.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-22 05:25:45",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mobyw",
"github_project": "nonebot-plugin-txt2img",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "nonebot-plugin-txt2img"
}