captionm 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- captionm-0.1.0/.github/workflows/publish.yml +31 -0
- captionm-0.1.0/.gitignore +223 -0
- captionm-0.1.0/LICENSE +21 -0
- captionm-0.1.0/PKG-INFO +67 -0
- captionm-0.1.0/README.md +55 -0
- captionm-0.1.0/pyproject.toml +20 -0
- captionm-0.1.0/src/captionm/__init__.py +46 -0
- captionm-0.1.0/src/captionm/discord_ext.py +130 -0
- captionm-0.1.0/src/captionm/emojis.py +96 -0
- captionm-0.1.0/src/captionm/engine.py +397 -0
- captionm-0.1.0/src/captionm/methods.py +377 -0
- captionm-0.1.0/src/captionm/styles.py +99 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
pypi-publish:
|
|
9
|
+
name: Build and publish to PyPI
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
environment:
|
|
12
|
+
name: pypi
|
|
13
|
+
url: https://pypi.org/p/captionm
|
|
14
|
+
permissions:
|
|
15
|
+
id-token: write
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- uses: actions/checkout@v7
|
|
19
|
+
|
|
20
|
+
- name: Set up Python
|
|
21
|
+
uses: actions/setup-python@v7
|
|
22
|
+
with:
|
|
23
|
+
python-version: "3.13"
|
|
24
|
+
|
|
25
|
+
- name: Build package
|
|
26
|
+
run: |
|
|
27
|
+
python -m pip install --upgrade pip build
|
|
28
|
+
python -m build
|
|
29
|
+
|
|
30
|
+
- name: Publish to PyPI
|
|
31
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
*.lcov
|
|
51
|
+
.hypothesis/
|
|
52
|
+
.pytest_cache/
|
|
53
|
+
cover/
|
|
54
|
+
|
|
55
|
+
# Translations
|
|
56
|
+
*.mo
|
|
57
|
+
*.pot
|
|
58
|
+
|
|
59
|
+
# Django stuff:
|
|
60
|
+
*.log
|
|
61
|
+
local_settings.py
|
|
62
|
+
db.sqlite3
|
|
63
|
+
db.sqlite3-journal
|
|
64
|
+
|
|
65
|
+
# Flask stuff:
|
|
66
|
+
instance/
|
|
67
|
+
.webassets-cache
|
|
68
|
+
|
|
69
|
+
# Scrapy stuff:
|
|
70
|
+
.scrapy
|
|
71
|
+
|
|
72
|
+
# Sphinx documentation
|
|
73
|
+
docs/_build/
|
|
74
|
+
|
|
75
|
+
# PyBuilder
|
|
76
|
+
.pybuilder/
|
|
77
|
+
target/
|
|
78
|
+
|
|
79
|
+
# Jupyter Notebook
|
|
80
|
+
.ipynb_checkpoints
|
|
81
|
+
|
|
82
|
+
# IPython
|
|
83
|
+
profile_default/
|
|
84
|
+
ipython_config.py
|
|
85
|
+
|
|
86
|
+
# pyenv
|
|
87
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
88
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
89
|
+
# .python-version
|
|
90
|
+
|
|
91
|
+
# pipenv
|
|
92
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
93
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
94
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
95
|
+
# install all needed dependencies.
|
|
96
|
+
# Pipfile.lock
|
|
97
|
+
|
|
98
|
+
# uv
|
|
99
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
100
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
101
|
+
# commonly ignored for libraries.
|
|
102
|
+
# uv.lock
|
|
103
|
+
|
|
104
|
+
# poetry
|
|
105
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
106
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
107
|
+
# commonly ignored for libraries.
|
|
108
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
109
|
+
# poetry.lock
|
|
110
|
+
# poetry.toml
|
|
111
|
+
|
|
112
|
+
# pdm
|
|
113
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
114
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
115
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
116
|
+
# pdm.lock
|
|
117
|
+
# pdm.toml
|
|
118
|
+
.pdm-python
|
|
119
|
+
.pdm-build/
|
|
120
|
+
|
|
121
|
+
# pixi
|
|
122
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
123
|
+
# pixi.lock
|
|
124
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
125
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
126
|
+
.pixi/*
|
|
127
|
+
!.pixi/config.toml
|
|
128
|
+
|
|
129
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
130
|
+
__pypackages__/
|
|
131
|
+
|
|
132
|
+
# Celery stuff
|
|
133
|
+
celerybeat-schedule*
|
|
134
|
+
celerybeat.pid
|
|
135
|
+
|
|
136
|
+
# Redis
|
|
137
|
+
*.rdb
|
|
138
|
+
*.aof
|
|
139
|
+
*.pid
|
|
140
|
+
|
|
141
|
+
# RabbitMQ
|
|
142
|
+
mnesia/
|
|
143
|
+
rabbitmq/
|
|
144
|
+
rabbitmq-data/
|
|
145
|
+
|
|
146
|
+
# ActiveMQ
|
|
147
|
+
activemq-data/
|
|
148
|
+
|
|
149
|
+
# SageMath parsed files
|
|
150
|
+
*.sage.py
|
|
151
|
+
|
|
152
|
+
# Environments
|
|
153
|
+
.env
|
|
154
|
+
.envrc
|
|
155
|
+
.venv
|
|
156
|
+
env/
|
|
157
|
+
venv/
|
|
158
|
+
ENV/
|
|
159
|
+
env.bak/
|
|
160
|
+
venv.bak/
|
|
161
|
+
|
|
162
|
+
# Conda environments
|
|
163
|
+
conda-meta/
|
|
164
|
+
|
|
165
|
+
# Spyder project settings
|
|
166
|
+
.spyderproject
|
|
167
|
+
.spyproject
|
|
168
|
+
|
|
169
|
+
# Rope project settings
|
|
170
|
+
.ropeproject
|
|
171
|
+
|
|
172
|
+
# mkdocs/Zensical documentation
|
|
173
|
+
/site
|
|
174
|
+
|
|
175
|
+
# mypy
|
|
176
|
+
.mypy_cache/
|
|
177
|
+
.dmypy.json
|
|
178
|
+
dmypy.json
|
|
179
|
+
|
|
180
|
+
# Pyre type checker
|
|
181
|
+
.pyre/
|
|
182
|
+
|
|
183
|
+
# pytype static type analyzer
|
|
184
|
+
.pytype/
|
|
185
|
+
|
|
186
|
+
# Cython debug symbols
|
|
187
|
+
cython_debug/
|
|
188
|
+
|
|
189
|
+
# PyCharm
|
|
190
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
191
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
192
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
193
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
194
|
+
# .idea/
|
|
195
|
+
|
|
196
|
+
# Abstra
|
|
197
|
+
# Abstra is an AI-powered process automation framework.
|
|
198
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
199
|
+
# Learn more at https://abstra.io/docs
|
|
200
|
+
.abstra/
|
|
201
|
+
|
|
202
|
+
# Visual Studio Code
|
|
203
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore that
|
|
204
|
+
# can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
205
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer, you
|
|
206
|
+
# could uncomment the following to ignore the entire vscode folder
|
|
207
|
+
# .vscode/
|
|
208
|
+
# Temporary file for partial code execution
|
|
209
|
+
tempCodeRunnerFile.py
|
|
210
|
+
|
|
211
|
+
# Ruff stuff:
|
|
212
|
+
.ruff_cache/
|
|
213
|
+
|
|
214
|
+
# PyPI configuration file
|
|
215
|
+
.pypirc
|
|
216
|
+
|
|
217
|
+
# Marimo
|
|
218
|
+
marimo/_static/
|
|
219
|
+
marimo/_lsp/
|
|
220
|
+
__marimo__/
|
|
221
|
+
|
|
222
|
+
# Streamlit
|
|
223
|
+
.streamlit/secrets.toml
|
captionm-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 thugyahu-ui
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
captionm-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: captionm
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Customizable media captioning engine for images, GIFs, and videos with discord.py support.
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Requires-Dist: av>=12.0.0
|
|
8
|
+
Requires-Dist: pillow>=10.0.0
|
|
9
|
+
Provides-Extra: heif
|
|
10
|
+
Requires-Dist: pillow-heif>=0.16.0; extra == 'heif'
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# captionm
|
|
14
|
+
|
|
15
|
+
simple python library for adding, removing, and replacing captions on images, gifs, and videos. works anywhere (web backends, scripts, discord bots, whatever).
|
|
16
|
+
|
|
17
|
+
## install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install captionm
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
needs python 3.10+. uses pillow and pyav under the hood.
|
|
24
|
+
|
|
25
|
+
## basic usage
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from captionm import caption_bytes, uncaption_bytes, recaption_bytes, CaptionStyle
|
|
29
|
+
|
|
30
|
+
with open("meme.gif", "rb") as f:
|
|
31
|
+
raw = f.read()
|
|
32
|
+
|
|
33
|
+
# add caption
|
|
34
|
+
buf, ext = caption_bytes(raw, "ohfr?")
|
|
35
|
+
|
|
36
|
+
# remove caption bar
|
|
37
|
+
buf, ext = uncaption_bytes(raw)
|
|
38
|
+
|
|
39
|
+
# replace caption
|
|
40
|
+
buf, ext = recaption_bytes(raw, "new text")
|
|
41
|
+
|
|
42
|
+
with open(f"out.{ext}", "wb") as f:
|
|
43
|
+
f.write(buf.read())
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## styling
|
|
47
|
+
|
|
48
|
+
pass `CaptionStyle` if you don't want the default white top bar:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
style = CaptionStyle(
|
|
52
|
+
font="path/to/font.otf",
|
|
53
|
+
text_color="white",
|
|
54
|
+
bg_color="black",
|
|
55
|
+
uppercase=True,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
buf, ext = caption_bytes(raw, "hello", method="bottom_bar", style=style)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
available methods: `top_bar`, `bottom_bar`, `overlay_top`, `overlay_bottom`, `overlay_center`, `area`.
|
|
62
|
+
|
|
63
|
+
## notes
|
|
64
|
+
|
|
65
|
+
- renders unicode emojis and custom discord emojis in color by default (`render_emojis=False` to turn off).
|
|
66
|
+
- if you have `discord.py` installed, `caption_message`, `uncaption_message`, and `recaption_message` are also exported.
|
|
67
|
+
- use `EngineLimits` if you need to cap resolution, frames, file size, or timeout.
|
captionm-0.1.0/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# captionm
|
|
2
|
+
|
|
3
|
+
simple python library for adding, removing, and replacing captions on images, gifs, and videos. works anywhere (web backends, scripts, discord bots, whatever).
|
|
4
|
+
|
|
5
|
+
## install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install captionm
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
needs python 3.10+. uses pillow and pyav under the hood.
|
|
12
|
+
|
|
13
|
+
## basic usage
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from captionm import caption_bytes, uncaption_bytes, recaption_bytes, CaptionStyle
|
|
17
|
+
|
|
18
|
+
with open("meme.gif", "rb") as f:
|
|
19
|
+
raw = f.read()
|
|
20
|
+
|
|
21
|
+
# add caption
|
|
22
|
+
buf, ext = caption_bytes(raw, "ohfr?")
|
|
23
|
+
|
|
24
|
+
# remove caption bar
|
|
25
|
+
buf, ext = uncaption_bytes(raw)
|
|
26
|
+
|
|
27
|
+
# replace caption
|
|
28
|
+
buf, ext = recaption_bytes(raw, "new text")
|
|
29
|
+
|
|
30
|
+
with open(f"out.{ext}", "wb") as f:
|
|
31
|
+
f.write(buf.read())
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## styling
|
|
35
|
+
|
|
36
|
+
pass `CaptionStyle` if you don't want the default white top bar:
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
style = CaptionStyle(
|
|
40
|
+
font="path/to/font.otf",
|
|
41
|
+
text_color="white",
|
|
42
|
+
bg_color="black",
|
|
43
|
+
uppercase=True,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
buf, ext = caption_bytes(raw, "hello", method="bottom_bar", style=style)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
available methods: `top_bar`, `bottom_bar`, `overlay_top`, `overlay_bottom`, `overlay_center`, `area`.
|
|
50
|
+
|
|
51
|
+
## notes
|
|
52
|
+
|
|
53
|
+
- renders unicode emojis and custom discord emojis in color by default (`render_emojis=False` to turn off).
|
|
54
|
+
- if you have `discord.py` installed, `caption_message`, `uncaption_message`, and `recaption_message` are also exported.
|
|
55
|
+
- use `EngineLimits` if you need to cap resolution, frames, file size, or timeout.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "captionm"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Customizable media captioning engine for images, GIFs, and videos with discord.py support."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"pillow>=10.0.0",
|
|
13
|
+
"av>=12.0.0",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[project.optional-dependencies]
|
|
17
|
+
heif = ["pillow-heif>=0.16.0"]
|
|
18
|
+
|
|
19
|
+
[tool.hatch.build.targets.wheel]
|
|
20
|
+
packages = ["src/captionm"]
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from .engine import caption_bytes, recaption_bytes, uncaption_bytes
|
|
2
|
+
from .methods import (
|
|
3
|
+
METHODS,
|
|
4
|
+
AreaRenderer,
|
|
5
|
+
BarRenderer,
|
|
6
|
+
CaptionRenderer,
|
|
7
|
+
OverlayRenderer,
|
|
8
|
+
RecaptionRenderer,
|
|
9
|
+
UncaptionRenderer,
|
|
10
|
+
)
|
|
11
|
+
from .styles import CaptionStyle, EngineLimits
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"caption_bytes",
|
|
15
|
+
"uncaption_bytes",
|
|
16
|
+
"recaption_bytes",
|
|
17
|
+
"CaptionStyle",
|
|
18
|
+
"EngineLimits",
|
|
19
|
+
"CaptionRenderer",
|
|
20
|
+
"BarRenderer",
|
|
21
|
+
"OverlayRenderer",
|
|
22
|
+
"AreaRenderer",
|
|
23
|
+
"UncaptionRenderer",
|
|
24
|
+
"RecaptionRenderer",
|
|
25
|
+
"METHODS",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
try:
|
|
29
|
+
from .discord_ext import (
|
|
30
|
+
caption_attachment,
|
|
31
|
+
caption_message,
|
|
32
|
+
recaption_attachment,
|
|
33
|
+
recaption_message,
|
|
34
|
+
uncaption_attachment,
|
|
35
|
+
uncaption_message,
|
|
36
|
+
)
|
|
37
|
+
__all__.extend([
|
|
38
|
+
"caption_attachment",
|
|
39
|
+
"caption_message",
|
|
40
|
+
"uncaption_attachment",
|
|
41
|
+
"uncaption_message",
|
|
42
|
+
"recaption_attachment",
|
|
43
|
+
"recaption_message",
|
|
44
|
+
])
|
|
45
|
+
except ImportError:
|
|
46
|
+
pass
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from dataclasses import replace
|
|
3
|
+
import re
|
|
4
|
+
from urllib.parse import urlparse
|
|
5
|
+
import aiohttp
|
|
6
|
+
import discord
|
|
7
|
+
from .engine import caption_bytes
|
|
8
|
+
from .methods import CaptionRenderer, RecaptionRenderer, UncaptionRenderer
|
|
9
|
+
from .styles import CaptionStyle, EngineLimits
|
|
10
|
+
|
|
11
|
+
_URL_RE = re.compile(r"https?://\S+")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _pick_media(msg: discord.Message) -> tuple[discord.Attachment | str | None, discord.Attachment | None, bool]:
|
|
15
|
+
media = font = None
|
|
16
|
+
force_gif = False
|
|
17
|
+
|
|
18
|
+
for att in msg.attachments:
|
|
19
|
+
if att.filename.lower().endswith((".ttf", ".otf")):
|
|
20
|
+
font = font or att
|
|
21
|
+
elif media is None:
|
|
22
|
+
media = att
|
|
23
|
+
|
|
24
|
+
if media is None:
|
|
25
|
+
for e in msg.embeds:
|
|
26
|
+
is_gifv = e.type == "gifv" or any(
|
|
27
|
+
host in (e.url or "").lower() for host in ("tenor.com", "giphy.com")
|
|
28
|
+
)
|
|
29
|
+
url = (e.video and e.video.url) if is_gifv else None
|
|
30
|
+
url = url or (e.image and e.image.url) or (e.video and e.video.url) or (e.thumbnail and e.thumbnail.url)
|
|
31
|
+
if url:
|
|
32
|
+
media = url
|
|
33
|
+
force_gif = is_gifv
|
|
34
|
+
break
|
|
35
|
+
|
|
36
|
+
if media is None and msg.stickers:
|
|
37
|
+
media = msg.stickers[0].url
|
|
38
|
+
|
|
39
|
+
if media is None and msg.content:
|
|
40
|
+
m = _URL_RE.search(msg.content)
|
|
41
|
+
if m:
|
|
42
|
+
media = m.group(0)
|
|
43
|
+
|
|
44
|
+
return media, font, force_gif
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
async def _run(
|
|
48
|
+
target: discord.Attachment | discord.Message,
|
|
49
|
+
text: str,
|
|
50
|
+
method: str | CaptionRenderer = "top_bar",
|
|
51
|
+
style: CaptionStyle | None = None,
|
|
52
|
+
limits: EngineLimits | None = None,
|
|
53
|
+
font_attachment: discord.Attachment | None = None,
|
|
54
|
+
) -> discord.File | None:
|
|
55
|
+
limits, style = limits or EngineLimits(), style or CaptionStyle()
|
|
56
|
+
force_gif = False
|
|
57
|
+
|
|
58
|
+
if isinstance(target, discord.Message):
|
|
59
|
+
source, font_att, force_gif = _pick_media(target)
|
|
60
|
+
if source is None and target.reference and target.reference.message_id:
|
|
61
|
+
replied = await target.channel.fetch_message(target.reference.message_id)
|
|
62
|
+
source, r_font, force_gif = _pick_media(replied)
|
|
63
|
+
font_att = font_att or r_font
|
|
64
|
+
if source is None:
|
|
65
|
+
return None
|
|
66
|
+
font_attachment = font_attachment or font_att
|
|
67
|
+
else:
|
|
68
|
+
source = target
|
|
69
|
+
|
|
70
|
+
if font_attachment is not None:
|
|
71
|
+
style = replace(style, font=await font_attachment.read())
|
|
72
|
+
|
|
73
|
+
if isinstance(source, discord.Attachment):
|
|
74
|
+
if limits.max_input_bytes is not None and source.size > limits.max_input_bytes:
|
|
75
|
+
raise ValueError(f"Attachment too large ({source.size} > {limits.max_input_bytes} bytes).")
|
|
76
|
+
raw, filename = await source.read(), source.filename
|
|
77
|
+
else:
|
|
78
|
+
async with aiohttp.ClientSession() as s, s.get(source, headers={"User-Agent": "captionm"}) as r:
|
|
79
|
+
r.raise_for_status()
|
|
80
|
+
raw = await r.read()
|
|
81
|
+
if limits.max_input_bytes is not None and len(raw) > limits.max_input_bytes:
|
|
82
|
+
raise ValueError(f"Media too large ({len(raw)} > {limits.max_input_bytes} bytes).")
|
|
83
|
+
filename = urlparse(source).path.rsplit("/", 1)[-1] or None
|
|
84
|
+
|
|
85
|
+
buf, ext = await asyncio.to_thread(
|
|
86
|
+
caption_bytes,
|
|
87
|
+
raw,
|
|
88
|
+
text,
|
|
89
|
+
method,
|
|
90
|
+
style,
|
|
91
|
+
limits,
|
|
92
|
+
filename,
|
|
93
|
+
"gif" if force_gif else None,
|
|
94
|
+
)
|
|
95
|
+
return discord.File(buf, filename=f"captioned.{ext}")
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
caption_attachment = _run
|
|
99
|
+
caption_message = _run
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
async def uncaption_attachment(
|
|
103
|
+
target: discord.Attachment | discord.Message,
|
|
104
|
+
position: str = "top",
|
|
105
|
+
tolerance: int = 40,
|
|
106
|
+
height: int | None = None,
|
|
107
|
+
limits: EngineLimits | None = None,
|
|
108
|
+
) -> discord.File | None:
|
|
109
|
+
return await _run(target, "", UncaptionRenderer(position, tolerance, height), limits=limits)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
uncaption_message = uncaption_attachment
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
async def recaption_attachment(
|
|
116
|
+
target: discord.Attachment | discord.Message,
|
|
117
|
+
text: str,
|
|
118
|
+
position: str = "top",
|
|
119
|
+
method: str | CaptionRenderer = "top_bar",
|
|
120
|
+
style: CaptionStyle | None = None,
|
|
121
|
+
tolerance: int = 40,
|
|
122
|
+
height: int | None = None,
|
|
123
|
+
limits: EngineLimits | None = None,
|
|
124
|
+
font_attachment: discord.Attachment | None = None,
|
|
125
|
+
) -> discord.File | None:
|
|
126
|
+
renderer = RecaptionRenderer(position, method, tolerance, height)
|
|
127
|
+
return await _run(target, text, renderer, style, limits, font_attachment)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
recaption_message = recaption_attachment
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
from functools import lru_cache
|
|
2
|
+
import io
|
|
3
|
+
import re
|
|
4
|
+
import urllib.request
|
|
5
|
+
from PIL import Image, ImageDraw
|
|
6
|
+
|
|
7
|
+
_DISCORD_EMOJI_RE = re.compile(r"<a?:[a-zA-Z0-9_]+:(\d+)>")
|
|
8
|
+
_UNICODE_EMOJI_RE = re.compile(
|
|
9
|
+
"["
|
|
10
|
+
"\U0001F1E0-\U0001F1FF"
|
|
11
|
+
"\U0001F300-\U0001F5FF"
|
|
12
|
+
"\U0001F600-\U0001F64F"
|
|
13
|
+
"\U0001F680-\U0001F6FF"
|
|
14
|
+
"\U0001F700-\U0001F77F"
|
|
15
|
+
"\U0001F900-\U0001F9FF"
|
|
16
|
+
"\U0001FA00-\U0001FAFF"
|
|
17
|
+
"\u2600-\u27BF"
|
|
18
|
+
"](?:\uFE0F|\U0001F3FB-\U0001F3FF|\u200D[\U0001F1E0-\U0001FAFF\u2600-\u27BF\uFE0F])*"
|
|
19
|
+
)
|
|
20
|
+
_TOKEN_RE = re.compile(f"({_DISCORD_EMOJI_RE.pattern}|{_UNICODE_EMOJI_RE.pattern})")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@lru_cache(maxsize=256)
|
|
24
|
+
def _fetch_emoji(url: str) -> Image.Image | None:
|
|
25
|
+
try:
|
|
26
|
+
req = urllib.request.Request(url, headers={"User-Agent": "captionm"})
|
|
27
|
+
with urllib.request.urlopen(req, timeout=5) as resp:
|
|
28
|
+
return Image.open(io.BytesIO(resp.read())).convert("RGBA")
|
|
29
|
+
except Exception:
|
|
30
|
+
return None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _get_emoji_image(token: str) -> Image.Image | None:
|
|
34
|
+
m = _DISCORD_EMOJI_RE.fullmatch(token)
|
|
35
|
+
if m:
|
|
36
|
+
return _fetch_emoji(f"https://cdn.discordapp.com/emojis/{m.group(1)}.png?size=128")
|
|
37
|
+
|
|
38
|
+
codepoints = "-".join(f"{ord(c):x}" for c in token if ord(c) != 0xFE0F)
|
|
39
|
+
return _fetch_emoji(
|
|
40
|
+
f"https://cdn.jsdelivr.net/gh/twitter/twemoji@14.0.2/assets/72x72/{codepoints}.png"
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def tokenize(text: str) -> list[tuple[bool, str]]:
|
|
45
|
+
tokens: list[tuple[bool, str]] = []
|
|
46
|
+
pos = 0
|
|
47
|
+
for m in _TOKEN_RE.finditer(text):
|
|
48
|
+
if m.start() > pos:
|
|
49
|
+
tokens.append((False, text[pos:m.start()]))
|
|
50
|
+
tokens.append((True, m.group(0)))
|
|
51
|
+
pos = m.end()
|
|
52
|
+
if pos < len(text):
|
|
53
|
+
tokens.append((False, text[pos:]))
|
|
54
|
+
return tokens
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def measure_line(draw: ImageDraw.ImageDraw, line: str, font, render_emojis: bool = True) -> float:
|
|
58
|
+
if not render_emojis:
|
|
59
|
+
return draw.textlength(line, font=font)
|
|
60
|
+
width = 0.0
|
|
61
|
+
emoji_size = int(font.size * 1.05)
|
|
62
|
+
for is_emoji, part in tokenize(line):
|
|
63
|
+
if is_emoji:
|
|
64
|
+
width += emoji_size
|
|
65
|
+
else:
|
|
66
|
+
width += draw.textlength(part, font=font)
|
|
67
|
+
return width
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def draw_line(
|
|
71
|
+
canvas: Image.Image,
|
|
72
|
+
draw: ImageDraw.ImageDraw,
|
|
73
|
+
pos: tuple[float, float],
|
|
74
|
+
line: str,
|
|
75
|
+
font,
|
|
76
|
+
fill,
|
|
77
|
+
stroke_width: int = 0,
|
|
78
|
+
stroke_fill=None,
|
|
79
|
+
render_emojis: bool = True,
|
|
80
|
+
) -> None:
|
|
81
|
+
x, y = pos
|
|
82
|
+
if not render_emojis:
|
|
83
|
+
draw.text((x, y), line, fill=fill, font=font, stroke_width=stroke_width, stroke_fill=stroke_fill)
|
|
84
|
+
return
|
|
85
|
+
|
|
86
|
+
emoji_size = int(font.size * 1.05)
|
|
87
|
+
for is_emoji, part in tokenize(line):
|
|
88
|
+
if is_emoji:
|
|
89
|
+
em = _get_emoji_image(part)
|
|
90
|
+
if em is not None:
|
|
91
|
+
resized = em.resize((emoji_size, emoji_size), Image.Resampling.LANCZOS)
|
|
92
|
+
canvas.paste(resized, (int(x), int(y)), resized)
|
|
93
|
+
x += emoji_size
|
|
94
|
+
continue
|
|
95
|
+
draw.text((x, y), part, fill=fill, font=font, stroke_width=stroke_width, stroke_fill=stroke_fill)
|
|
96
|
+
x += draw.textlength(part, font=font)
|