pil-utils


Namepil-utils JSON
Version 0.1.10 PyPI version JSON
download
home_pagehttps://github.com/MeetWq/pil-utils
SummaryA simple PIL wrapper and text-to-image tool
upload_time2024-03-07 13:39:29
maintainer
docs_urlNone
authormeetwq
requires_python>=3.8,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## pil-utils


### 功能

- 提供 `BuildImage` 类,方便图片尺寸修改、添加文字等操作
- 提供 `Text2Image` 类,方便实现文字转图,支持少量 `BBCode` 标签
- 文字支持多种字体切换,能够支持 `emoji`
- 添加文字自动调节字体大小


### 配置字体

字体文件需要安装到系统目录下

> **Note**
>
> 安装新字体后若文字仍显示不正常,可删掉 `matplotlib` 字体缓存文件重新运行程序
>
> 缓存文件位置:
> - Windows: `C:\Users\<username>\.matplotlib\fontlist-xxx.json`
> - Linux: `~/.cache/matplotlib/fontlist-xxx.json`
> - Mac: `~/Library/Caches/matplotlib/fontlist-xxx.json`


默认从以下备选字体列表中查找能够显示的字体

```
"Arial", "Tahoma", "Helvetica Neue", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Source Han Sans SC", "Noto Sans SC", "Noto Sans CJK SC", "WenQuanYi Micro Hei", "Apple Color Emoji", "Noto Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"
```

#### 中文字体 和 emoji字体 安装

根据系统的不同,推荐安装的字体如下:

- Windows:

大部分 Windows 系统自带 [微软雅黑](https://learn.microsoft.com/zh-cn/typography/font-list/microsoft-yahei) 中文字体 和 [Segoe UI Emoji](https://learn.microsoft.com/zh-cn/typography/font-list/segoe-ui-emoji) emoji 字体,一般情况下无需额外安装


- Linux:

部分系统可能自带 [文泉驿微米黑](http://wenq.org/wqy2/index.cgi?MicroHei) 中文字体;

对于 Ubuntu 系统,推荐安装 Noto Sans CJK 和 Noto Color Emoji:

```bash
sudo apt install fonts-noto-cjk fonts-noto-color-emoji
```

为避免 Noto Sans CJK 中部分中文显示为异体(日文)字形,可以将简体中文设置为默认语言(详见 [ArchWiki](https://wiki.archlinux.org/title/Localization/Simplified_Chinese?rdfrom=https%3A%2F%2Fwiki.archlinux.org%2Findex.php%3Ftitle%3DLocalization_%28%25E7%25AE%2580%25E4%25BD%2593%25E4%25B8%25AD%25E6%2596%2587%29%2FSimplified_Chinese_%28%25E7%25AE%2580%25E4%25BD%2593%25E4%25B8%25AD%25E6%2596%2587%29%26redirect%3Dno#%E4%BF%AE%E6%AD%A3%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87%E6%98%BE%E7%A4%BA%E4%B8%BA%E5%BC%82%E4%BD%93%EF%BC%88%E6%97%A5%E6%96%87%EF%BC%89%E5%AD%97%E5%BD%A2)):

```bash
sudo locale-gen zh_CN zh_CN.UTF-8
sudo update-locale LC_ALL=zh_CN.UTF-8 LANG=zh_CN.UTF-8
fc-cache -fv
```

其他 Linux 系统可以自行下载字体文件安装:

思源黑体:https://github.com/adobe-fonts/source-han-sans

NotoSansSC:https://fonts.google.com/noto/specimen/Noto+Sans+SC

Noto Color Emoji:https://github.com/googlefonts/noto-emoji


- Mac:

苹果系统一般自带 "PingFang SC" 中文字体 与 "Apple Color Emoji" emoji 字体

#### 字体安装方式

不同系统的字体安装方式:

- Windows:
    - 双击通过字体查看器安装
    - 复制到字体文件夹:`C:\Windows\Fonts`

- Linux:

在 `/usr/share/fonts` 目录下新建文件夹,如 `myfonts`,将字体文件复制到该路径下;

运行如下命令建立字体缓存:

```bash
fc-cache -fv
```

- Mac:

使用字体册打开字体文件安装


### 使用示例


- `BuildImage`

```python
from pil_utils import BuildImage

# output: BytesIO
output = BuildImage.new("RGBA", (200, 200), "grey").circle().draw_text((0, 0, 200, 200), "测试test😂").save_png()
```

![](https://s2.loli.net/2023/02/17/oOjw9sSbfDAJvYr.png)


- `Text2Image`

```python
from pil_utils import Text2Image

# img: PIL.Image.Image
img = Text2Image.from_text("@mnixry 🤗", 50).to_image(bg_color="white")
```

![](https://s2.loli.net/2023/02/06/aJTqGwzvsVBSO8H.png)


- 使用 `BBCode`

```python
from pil_utils import text2image

# img: PIL.Image.Image
img = text2image("N[size=40][color=red]O[/color][/size]neBo[size=40][color=blue]T[/color][/size]\n[align=center]太强啦[/align]")
```

![](https://s2.loli.net/2023/02/06/Hfwj67QoVAatexN.png)


目前支持的 `BBCode` 标签:
- `[align=left|right|center][/align]`: 文字对齐方式
- `[color=#66CCFF|red|black][/color]`: 字体颜色
- `[stroke=#66CCFF|red|black][/stroke]`: 描边颜色
- `[font=Microsoft YaHei][/font]`: 文字字体
- `[size=30][/size]`: 文字大小
- `[b][/b]`: 文字加粗
- `[i][/i]`: 文字斜体
- `[u][/u]`: 文字下划线
- `[del][/del]`: 文字删除线

### 特别感谢

- [HibiKier/zhenxun_bot](https://github.com/HibiKier/zhenxun_bot) 基于 Nonebot2 和 go-cqhttp 开发,以 postgresql 作为数据库,非常可爱的绪山真寻bot

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/MeetWq/pil-utils",
    "name": "pil-utils",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "meetwq",
    "author_email": "meetwq@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/b6/a7/bcffcc9fdd906c5f123c63afc001469d20bfb2723b8b7fe1fb7f9def55b0/pil_utils-0.1.10.tar.gz",
    "platform": null,
    "description": "## pil-utils\n\n\n### \u529f\u80fd\n\n- \u63d0\u4f9b `BuildImage` \u7c7b\uff0c\u65b9\u4fbf\u56fe\u7247\u5c3a\u5bf8\u4fee\u6539\u3001\u6dfb\u52a0\u6587\u5b57\u7b49\u64cd\u4f5c\n- \u63d0\u4f9b `Text2Image` \u7c7b\uff0c\u65b9\u4fbf\u5b9e\u73b0\u6587\u5b57\u8f6c\u56fe\uff0c\u652f\u6301\u5c11\u91cf `BBCode` \u6807\u7b7e\n- \u6587\u5b57\u652f\u6301\u591a\u79cd\u5b57\u4f53\u5207\u6362\uff0c\u80fd\u591f\u652f\u6301 `emoji`\n- \u6dfb\u52a0\u6587\u5b57\u81ea\u52a8\u8c03\u8282\u5b57\u4f53\u5927\u5c0f\n\n\n### \u914d\u7f6e\u5b57\u4f53\n\n\u5b57\u4f53\u6587\u4ef6\u9700\u8981\u5b89\u88c5\u5230\u7cfb\u7edf\u76ee\u5f55\u4e0b\n\n> **Note**\n>\n> \u5b89\u88c5\u65b0\u5b57\u4f53\u540e\u82e5\u6587\u5b57\u4ecd\u663e\u793a\u4e0d\u6b63\u5e38\uff0c\u53ef\u5220\u6389 `matplotlib` \u5b57\u4f53\u7f13\u5b58\u6587\u4ef6\u91cd\u65b0\u8fd0\u884c\u7a0b\u5e8f\n>\n> \u7f13\u5b58\u6587\u4ef6\u4f4d\u7f6e\uff1a\n> - Windows: `C:\\Users\\<username>\\.matplotlib\\fontlist-xxx.json`\n> - Linux: `~/.cache/matplotlib/fontlist-xxx.json`\n> - Mac: `~/Library/Caches/matplotlib/fontlist-xxx.json`\n\n\n\u9ed8\u8ba4\u4ece\u4ee5\u4e0b\u5907\u9009\u5b57\u4f53\u5217\u8868\u4e2d\u67e5\u627e\u80fd\u591f\u663e\u793a\u7684\u5b57\u4f53\n\n```\n\"Arial\", \"Tahoma\", \"Helvetica Neue\", \"Segoe UI\", \"PingFang SC\", \"Hiragino Sans GB\", \"Microsoft YaHei\", \"Source Han Sans SC\", \"Noto Sans SC\", \"Noto Sans CJK SC\", \"WenQuanYi Micro Hei\", \"Apple Color Emoji\", \"Noto Color Emoji\", \"Segoe UI Emoji\", \"Segoe UI Symbol\"\n```\n\n#### \u4e2d\u6587\u5b57\u4f53 \u548c emoji\u5b57\u4f53 \u5b89\u88c5\n\n\u6839\u636e\u7cfb\u7edf\u7684\u4e0d\u540c\uff0c\u63a8\u8350\u5b89\u88c5\u7684\u5b57\u4f53\u5982\u4e0b\uff1a\n\n- Windows:\n\n\u5927\u90e8\u5206 Windows \u7cfb\u7edf\u81ea\u5e26 [\u5fae\u8f6f\u96c5\u9ed1](https://learn.microsoft.com/zh-cn/typography/font-list/microsoft-yahei) \u4e2d\u6587\u5b57\u4f53 \u548c [Segoe UI Emoji](https://learn.microsoft.com/zh-cn/typography/font-list/segoe-ui-emoji) emoji \u5b57\u4f53\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u65e0\u9700\u989d\u5916\u5b89\u88c5\n\n\n- Linux:\n\n\u90e8\u5206\u7cfb\u7edf\u53ef\u80fd\u81ea\u5e26 [\u6587\u6cc9\u9a7f\u5fae\u7c73\u9ed1](http://wenq.org/wqy2/index.cgi?MicroHei) \u4e2d\u6587\u5b57\u4f53\uff1b\n\n\u5bf9\u4e8e Ubuntu \u7cfb\u7edf\uff0c\u63a8\u8350\u5b89\u88c5 Noto Sans CJK \u548c Noto Color Emoji\uff1a\n\n```bash\nsudo apt install fonts-noto-cjk fonts-noto-color-emoji\n```\n\n\u4e3a\u907f\u514d Noto Sans CJK \u4e2d\u90e8\u5206\u4e2d\u6587\u663e\u793a\u4e3a\u5f02\u4f53\uff08\u65e5\u6587\uff09\u5b57\u5f62\uff0c\u53ef\u4ee5\u5c06\u7b80\u4f53\u4e2d\u6587\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u8bed\u8a00\uff08\u8be6\u89c1 [ArchWiki](https://wiki.archlinux.org/title/Localization/Simplified_Chinese?rdfrom=https%3A%2F%2Fwiki.archlinux.org%2Findex.php%3Ftitle%3DLocalization_%28%25E7%25AE%2580%25E4%25BD%2593%25E4%25B8%25AD%25E6%2596%2587%29%2FSimplified_Chinese_%28%25E7%25AE%2580%25E4%25BD%2593%25E4%25B8%25AD%25E6%2596%2587%29%26redirect%3Dno#%E4%BF%AE%E6%AD%A3%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87%E6%98%BE%E7%A4%BA%E4%B8%BA%E5%BC%82%E4%BD%93%EF%BC%88%E6%97%A5%E6%96%87%EF%BC%89%E5%AD%97%E5%BD%A2)\uff09\uff1a\n\n```bash\nsudo locale-gen zh_CN zh_CN.UTF-8\nsudo update-locale LC_ALL=zh_CN.UTF-8 LANG=zh_CN.UTF-8\nfc-cache -fv\n```\n\n\u5176\u4ed6 Linux \u7cfb\u7edf\u53ef\u4ee5\u81ea\u884c\u4e0b\u8f7d\u5b57\u4f53\u6587\u4ef6\u5b89\u88c5\uff1a\n\n\u601d\u6e90\u9ed1\u4f53\uff1ahttps://github.com/adobe-fonts/source-han-sans\n\nNotoSansSC\uff1ahttps://fonts.google.com/noto/specimen/Noto+Sans+SC\n\nNoto Color Emoji\uff1ahttps://github.com/googlefonts/noto-emoji\n\n\n- Mac:\n\n\u82f9\u679c\u7cfb\u7edf\u4e00\u822c\u81ea\u5e26 \"PingFang SC\" \u4e2d\u6587\u5b57\u4f53 \u4e0e \"Apple Color Emoji\" emoji \u5b57\u4f53\n\n#### \u5b57\u4f53\u5b89\u88c5\u65b9\u5f0f\n\n\u4e0d\u540c\u7cfb\u7edf\u7684\u5b57\u4f53\u5b89\u88c5\u65b9\u5f0f\uff1a\n\n- Windows:\n    - \u53cc\u51fb\u901a\u8fc7\u5b57\u4f53\u67e5\u770b\u5668\u5b89\u88c5\n    - \u590d\u5236\u5230\u5b57\u4f53\u6587\u4ef6\u5939\uff1a`C:\\Windows\\Fonts`\n\n- Linux:\n\n\u5728 `/usr/share/fonts` \u76ee\u5f55\u4e0b\u65b0\u5efa\u6587\u4ef6\u5939\uff0c\u5982 `myfonts`\uff0c\u5c06\u5b57\u4f53\u6587\u4ef6\u590d\u5236\u5230\u8be5\u8def\u5f84\u4e0b\uff1b\n\n\u8fd0\u884c\u5982\u4e0b\u547d\u4ee4\u5efa\u7acb\u5b57\u4f53\u7f13\u5b58\uff1a\n\n```bash\nfc-cache -fv\n```\n\n- Mac:\n\n\u4f7f\u7528\u5b57\u4f53\u518c\u6253\u5f00\u5b57\u4f53\u6587\u4ef6\u5b89\u88c5\n\n\n### \u4f7f\u7528\u793a\u4f8b\n\n\n- `BuildImage`\n\n```python\nfrom pil_utils import BuildImage\n\n# output: BytesIO\noutput = BuildImage.new(\"RGBA\", (200, 200), \"grey\").circle().draw_text((0, 0, 200, 200), \"\u6d4b\u8bd5test\ud83d\ude02\").save_png()\n```\n\n![](https://s2.loli.net/2023/02/17/oOjw9sSbfDAJvYr.png)\n\n\n- `Text2Image`\n\n```python\nfrom pil_utils import Text2Image\n\n# img: PIL.Image.Image\nimg = Text2Image.from_text(\"@mnixry \ud83e\udd17\", 50).to_image(bg_color=\"white\")\n```\n\n![](https://s2.loli.net/2023/02/06/aJTqGwzvsVBSO8H.png)\n\n\n- \u4f7f\u7528 `BBCode`\n\n```python\nfrom pil_utils import text2image\n\n# img: PIL.Image.Image\nimg = text2image(\"N[size=40][color=red]O[/color][/size]neBo[size=40][color=blue]T[/color][/size]\\n[align=center]\u592a\u5f3a\u5566[/align]\")\n```\n\n![](https://s2.loli.net/2023/02/06/Hfwj67QoVAatexN.png)\n\n\n\u76ee\u524d\u652f\u6301\u7684 `BBCode` \u6807\u7b7e\uff1a\n- `[align=left|right|center][/align]`: \u6587\u5b57\u5bf9\u9f50\u65b9\u5f0f\n- `[color=#66CCFF|red|black][/color]`: \u5b57\u4f53\u989c\u8272\n- `[stroke=#66CCFF|red|black][/stroke]`: \u63cf\u8fb9\u989c\u8272\n- `[font=Microsoft YaHei][/font]`: \u6587\u5b57\u5b57\u4f53\n- `[size=30][/size]`: \u6587\u5b57\u5927\u5c0f\n- `[b][/b]`: \u6587\u5b57\u52a0\u7c97\n- `[i][/i]`: \u6587\u5b57\u659c\u4f53\n- `[u][/u]`: \u6587\u5b57\u4e0b\u5212\u7ebf\n- `[del][/del]`: \u6587\u5b57\u5220\u9664\u7ebf\n\n### \u7279\u522b\u611f\u8c22\n\n- [HibiKier/zhenxun_bot](https://github.com/HibiKier/zhenxun_bot) \u57fa\u4e8e Nonebot2 \u548c go-cqhttp \u5f00\u53d1\uff0c\u4ee5 postgresql \u4f5c\u4e3a\u6570\u636e\u5e93\uff0c\u975e\u5e38\u53ef\u7231\u7684\u7eea\u5c71\u771f\u5bfbbot\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A simple PIL wrapper and text-to-image tool",
    "version": "0.1.10",
    "project_urls": {
        "Homepage": "https://github.com/MeetWq/pil-utils",
        "Repository": "https://github.com/MeetWq/pil-utils"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "92ecb7c7a3330a158bd5520b89d87903c298d9ba3b4f4ff39c54b55090d8bf69",
                "md5": "60cc77116f7b8989087e45d6097dbd05",
                "sha256": "ff9459d7b4336e9b07a5e11e06b12a6e477ae68eae0124ac5710b28ae5d4d6d8"
            },
            "downloads": -1,
            "filename": "pil_utils-0.1.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "60cc77116f7b8989087e45d6097dbd05",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 17050,
            "upload_time": "2024-03-07T13:39:23",
            "upload_time_iso_8601": "2024-03-07T13:39:23.814159Z",
            "url": "https://files.pythonhosted.org/packages/92/ec/b7c7a3330a158bd5520b89d87903c298d9ba3b4f4ff39c54b55090d8bf69/pil_utils-0.1.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b6a7bcffcc9fdd906c5f123c63afc001469d20bfb2723b8b7fe1fb7f9def55b0",
                "md5": "07ed938e6c784ea21db138d894038d07",
                "sha256": "4e6d08cc39cba3f65eb17847662d2c1e68e2ac12503e4a73e57d980b2dc77cda"
            },
            "downloads": -1,
            "filename": "pil_utils-0.1.10.tar.gz",
            "has_sig": false,
            "md5_digest": "07ed938e6c784ea21db138d894038d07",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 16628,
            "upload_time": "2024-03-07T13:39:29",
            "upload_time_iso_8601": "2024-03-07T13:39:29.052951Z",
            "url": "https://files.pythonhosted.org/packages/b6/a7/bcffcc9fdd906c5f123c63afc001469d20bfb2723b8b7fe1fb7f9def55b0/pil_utils-0.1.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-07 13:39:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "MeetWq",
    "github_project": "pil-utils",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pil-utils"
}
        
Elapsed time: 0.20543s