Giter VIP home page Giter VIP logo

pil-utils's Introduction

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 系统自带 微软雅黑 中文字体 和 Segoe UI Emoji emoji 字体,一般情况下无需额外安装

  • Linux:

部分系统可能自带 文泉驿微米黑 中文字体;

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

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

为避免 Noto Sans CJK 中部分中文显示为异体(日文)字形,可以将简体中文设置为默认语言(详见 ArchWiki):

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,将字体文件复制到该路径下;

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

fc-cache -fv
  • Mac:

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

使用示例

  • BuildImage
from pil_utils import BuildImage

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

  • Text2Image
from pil_utils import Text2Image

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

  • 使用 BBCode
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]")

目前支持的 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 基于 Nonebot2 和 go-cqhttp 开发,以 postgresql 作为数据库,非常可爱的绪山真寻bot

pil-utils's People

Contributors

github-actions[bot] avatar meetwq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

vincentzyx

pil-utils's Issues

没有指定字体时绘制文字超级耗时

https://github.com/lgc-NB2Dev/nonebot-plugin-multincm
我在使用这个点歌插件时发现在docker上绘制歌曲列表耗时10s,绘制歌词耗时至少30s,使用 viztrace 进行性能分析后发现最大的耗时在 pil_utils 在绘制文字函数内,具体的说是 Text2Image.from_bbcode_text() 函数里调用的 get_proper_font(),我认为可以创建一个 Dict, 缓存所有的 Font 对象,这样可能会多占用一些内存,但是不会出现频繁切换 Font 耗时巨大的问题了

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.