Horoscope/main.py

153 lines
4 KiB
Python
Raw Permalink Normal View History

2024-09-29 00:38:21 +02:00
from os import makedirs
2024-09-29 01:01:56 +02:00
from os.path import isfile, join
from pathlib import Path
2024-09-29 00:38:21 +02:00
from random import choice
from sys import argv
from urllib.request import urlretrieve
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont
2024-09-28 20:35:08 +02:00
2024-09-29 00:38:21 +02:00
zodiac_signs = [
"Belier",
"Cancer",
"Balance",
"Capricorne",
"Taureau",
"Lion",
"Scorpion",
"Verseau",
"Gemeaux",
"Vierge",
"Sagittaire",
"Poissons",
]
2024-09-28 23:24:05 +02:00
2024-09-29 01:01:56 +02:00
image_dir = join(Path(argv[0]).parent.resolve(), "images")
2024-09-29 00:38:21 +02:00
def generate_horoscope() -> dict[str, dict[str, str]]:
"""Generate horoscope predictions"""
2024-09-28 20:35:08 +02:00
horoscope = {}
for sign in zodiac_signs:
2024-09-28 23:24:05 +02:00
love = choice(["+++", "++", "+", "-", "- -"])
2024-09-28 20:49:20 +02:00
work = choice(["+++", "++", "+", "-", "- -"])
2024-09-28 23:24:05 +02:00
horoscope[sign] = {"love": love, "work": work}
2024-09-28 20:35:08 +02:00
return horoscope
2024-09-29 00:38:21 +02:00
def get_path(image: str):
2024-09-29 01:01:56 +02:00
"""Return the path of an image"""
return join(image_dir, f"{image.lower()}.png")
2024-09-29 00:38:21 +02:00
def get_sign_image(image: str):
"""Get sign image"""
sign_image = Image.open(get_path(image)).convert("RGBA")
# Add thickness
alpha = sign_image.split()[3]
bold_mask = alpha.filter(ImageFilter.MaxFilter())
sign_image.putalpha(bold_mask)
# Add contrast
enhancer = ImageEnhance.Contrast(sign_image)
enhanced_image = enhancer.enhance(2.0)
enhanced_image.thumbnail((100, 105))
return enhanced_image
2024-10-03 21:11:48 +02:00
def vertical_text(text: str, font: ImageFont.ImageFont | ImageFont.FreeTypeFont):
"""Image with vertical text"""
text_img = Image.new("RGBA", (135, 24), (255, 255, 255, 0))
text_draw = ImageDraw.Draw(text_img)
text_draw.text((0, 0), f"{text:^15}", font=font, fill="black")
text_draw = text_img.rotate(90, expand=True)
return text_draw
2024-09-28 23:24:05 +02:00
def create_horoscope_image(horoscope: dict[str, dict[str, str]]):
2024-09-29 00:38:21 +02:00
"""Generate horoscope images"""
2024-09-28 23:24:05 +02:00
img_width, img_height = 1200, 425
2024-09-28 20:49:20 +02:00
image = Image.new("RGBA", (img_width, img_height), color=(255, 255, 255, 0))
draw = ImageDraw.Draw(image)
2024-09-28 20:35:08 +02:00
2024-09-28 23:24:05 +02:00
size_emoji = (30, 30)
font_sign = ImageFont.load_default(24)
font_prediction = ImageFont.load_default(40)
2024-09-28 20:35:08 +02:00
2024-09-28 20:49:20 +02:00
x, y = 10, 10
2024-09-28 20:35:08 +02:00
for sign, prediction in horoscope.items():
sign_image = get_sign_image(sign)
2024-09-28 23:24:05 +02:00
image.paste(sign_image, (x + 20, y), sign_image)
2024-09-28 20:49:20 +02:00
2024-09-28 23:24:05 +02:00
# Sign text
2024-10-03 21:11:48 +02:00
text_draw = vertical_text(sign, font_sign)
2024-09-28 23:24:05 +02:00
image.paste(text_draw, (x - 10, y - 10), text_draw)
# Love prediction
2024-09-29 01:01:56 +02:00
love = Image.open(join(image_dir, "love.png")).convert("RGBA")
2024-09-28 23:24:05 +02:00
love.thumbnail(size_emoji)
image.paste(love, (x + 125, y + 20), love)
2024-09-28 20:49:20 +02:00
draw.text(
2024-09-28 23:24:05 +02:00
(x + 160, y + 10),
prediction["love"],
fill="black",
font=font_prediction,
2024-09-28 20:49:20 +02:00
)
2024-09-28 23:24:05 +02:00
# Work prediction
2024-09-29 01:01:56 +02:00
work = Image.open(join(image_dir, "work.png")).convert("RGBA")
2024-09-28 23:24:05 +02:00
work.thumbnail(size_emoji)
image.paste(work, (x + 125, y + 60), work)
2024-09-28 20:49:20 +02:00
draw.text(
2024-09-28 23:24:05 +02:00
(x + 160, y + 50),
prediction["work"],
fill="black",
font=font_prediction,
2024-09-28 20:49:20 +02:00
)
2024-09-28 23:24:05 +02:00
x += 320
if x > 1200:
2024-09-28 20:49:20 +02:00
x = 10
2024-09-28 20:35:08 +02:00
y += 150
return image
2024-09-29 00:38:21 +02:00
def download_images():
"""Download images from source"""
url = "https://git.mylloon.fr/Anri/Horoscope/raw/branch/main/images/"
2024-09-29 01:01:56 +02:00
makedirs(image_dir, exist_ok=True)
2024-09-29 00:38:21 +02:00
for el in zodiac_signs + ["love", "work"]:
2024-09-29 01:01:56 +02:00
path = get_path(el)
if not isfile(path):
image = el.lower() + ".png"
print(f"Download {image}...")
urlretrieve(url + image, path)
2024-09-29 00:38:21 +02:00
2024-09-28 20:49:20 +02:00
if __name__ == "__main__":
2024-09-29 00:38:21 +02:00
if len(argv) > 2 or (len(argv) == 2 and argv[1] != "online"):
exit(1)
if len(argv) == 2:
2024-09-29 01:01:56 +02:00
print("Fetch missing images...")
2024-09-29 00:38:21 +02:00
download_images()
2024-09-28 20:49:20 +02:00
# Generate new horoscope
new_horoscope = generate_horoscope()
2024-09-28 20:35:08 +02:00
2024-09-28 20:49:20 +02:00
# Create and save the image
horoscope_image = create_horoscope_image(new_horoscope)
2024-09-28 20:35:08 +02:00
2024-09-29 01:01:56 +02:00
png = "nouvel_horoscope.png"
horoscope_image.save(png)
print(f"Nouvel horoscope généré et sauvegardé sous '{png}'")