1
Fork 0

Initial version

This commit is contained in:
Jake Howard 2024-12-07 18:02:36 +00:00
commit 987d16a617
Signed by: jake
GPG key ID: 57AFB45680EDD477
3 changed files with 294 additions and 0 deletions

178
.gitignore vendored Normal file
View file

@ -0,0 +1,178 @@
# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
### Python Patch ###
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
poetry.toml
# ruff
.ruff_cache/
# LSP config files
pyrightconfig.json
# End of https://www.toptal.com/developers/gitignore/api/python
image.*

113
main.py Normal file
View file

@ -0,0 +1,113 @@
from PIL import Image, ImageDraw, ImageFont
import requests
from resizeimage import resizeimage
OUTPUT_RESOLUTION = (1280, 720)
TITLE_FONT = ImageFont.truetype("Roboto-Regular.ttf", 92)
TAGS_FONT = ImageFont.truetype("RobotoMono-Regular.ttf", 40)
FOOTER_FONT = ImageFont.truetype("RobotoMono-Regular.ttf", 25)
TITLE_PADDING = 75
TITLE = "Accessing Tailscale whilst using Mullvad"
TAGS = "#linux #containers #foobar"
def get_text_width(text, font):
return draw.multiline_textbbox((0, 0), text=text, font=font)[2]
def split_words_for_length(text, font, max_width):
"""
Copied from mkdocs-material social plugin.
"""
lines = []
words = []
for word in text.split(" "):
combine = " ".join(words + [word])
width = get_text_width(combine, font=font)
if not words or width <= max_width:
words.append(word)
else:
lines.append(words)
words = [word]
lines.append(words)
return [" ".join(line) for line in lines]
print("Requesting")
bg_image_request = requests.get(
"https://images.unsplash.com/photo-1544957264-d520d519a8d5?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzNjExNzd8MHwxfGFsbHx8fHx8fHx8fDE3MzMzNTY4MjB8&ixlib=rb-4.0.3&q=85",
stream=True,
)
bg_image_request.raise_for_status()
print("Opening")
bg_image = Image.open(bg_image_request.raw)
print("Resizing")
bg_image = resizeimage.resize_cover(bg_image, OUTPUT_RESOLUTION)
draw = ImageDraw.Draw(bg_image, "RGBA")
print("Drawing title")
draw.rectangle([(0, 0), OUTPUT_RESOLUTION], fill=(0, 0, 0, int(255 * 0.66)))
title_lines = split_words_for_length(
TITLE, TITLE_FONT, OUTPUT_RESOLUTION[0] - (TITLE_PADDING * 2)
)
draw.text(
(OUTPUT_RESOLUTION[0] / 2, OUTPUT_RESOLUTION[1] / 2),
text="\n".join(title_lines),
font=TITLE_FONT,
fill="white",
anchor="mm",
align="center",
)
print("Drawing tags")
title_top = (OUTPUT_RESOLUTION[1] / 2) - (TITLE_FONT.size * len(title_lines) / 2)
tags_lines = split_words_for_length(
TAGS, TAGS_FONT, OUTPUT_RESOLUTION[0] - (TITLE_PADDING * 2)
)
draw.text(
(OUTPUT_RESOLUTION[0] / 2, title_top - (TAGS_FONT.size * 1.25)),
text=TAGS,
font=TAGS_FONT,
fill="#e85537",
anchor="mm",
align="center",
)
print("Drawing footer")
draw.rectangle(
[(0, OUTPUT_RESOLUTION[1] - 50), OUTPUT_RESOLUTION], fill=(0, 0, 0, int(255 * 0.5))
)
draw.text(
(25, OUTPUT_RESOLUTION[1] - 25),
text="theorangeone.net",
font=FOOTER_FONT,
fill="#e85537",
anchor="lm",
align="center",
)
draw.text(
(OUTPUT_RESOLUTION[0] - 25, OUTPUT_RESOLUTION[1] - 25),
text="2024-01-01",
font=FOOTER_FONT,
fill="white",
anchor="rm",
align="right",
)
print("Saving")
with open(f"image.{bg_image.format.lower()}", "wb") as f:
bg_image.save(f, optimize=True)

3
requirements.txt Normal file
View file

@ -0,0 +1,3 @@
pillow
requests
python-resize-image