video-stream/driver/design/thumbnail.py

67 lines
2.1 KiB
Python
Raw Permalink Normal View History

2022-01-31 12:01:11 +00:00
import os
import aiofiles
import aiohttp
2022-02-07 16:41:20 +00:00
from PIL import (
Image,
ImageDraw,
ImageFont,
)
2022-01-31 12:01:11 +00:00
def changeImageSize(maxWidth, maxHeight, image):
2022-02-13 08:22:10 +00:00
if image.size[0] == image.size[1]:
# Does not change the scale of the orientation image and displays it centered.
# It may look even better
newImage = image.resize((maxHeight, maxHeight))
img = Image.new("RGBA", (maxWidth, maxHeight))
img.paste(newImage, (int((maxWidth - maxHeight) / 2), 0))
return img
else:
widthRatio = maxWidth / image.size[0]
heightRatio = maxHeight / image.size[1]
newWidth = int(widthRatio * image.size[0])
newHeight = int(heightRatio * image.size[1])
newImage = image.resize((newWidth, newHeight))
2022-01-31 12:01:11 +00:00
return newImage
async def thumb(thumbnail, title, userid, ctitle):
2022-02-13 08:22:10 +00:00
img_path = f"search/thumb{userid}.png"
if 'http' in thumbnail:
async with aiohttp.ClientSession() as session:
async with session.get(thumbnail) as resp:
if resp.status == 200:
f = await aiofiles.open(img_path, mode="wb")
await f.write(await resp.read())
await f.close()
else:
img_path = thumbnail
image1 = Image.open(img_path)
2022-02-10 21:38:40 +00:00
image2 = Image.open("driver/source/LightGreen.png")
2022-01-31 12:01:11 +00:00
image3 = changeImageSize(1280, 720, image1)
image4 = changeImageSize(1280, 720, image2)
image5 = image3.convert("RGBA")
image6 = image4.convert("RGBA")
Image.alpha_composite(image5, image6).save(f"search/temp{userid}.png")
img = Image.open(f"search/temp{userid}.png")
draw = ImageDraw.Draw(img)
2022-02-03 13:57:42 +00:00
font = ImageFont.truetype("driver/source/regular.ttf", 50)
font2 = ImageFont.truetype("driver/source/medium.ttf", 72)
2022-01-31 12:01:11 +00:00
draw.text(
2022-02-03 13:57:42 +00:00
(25, 615),
f"{title[:20]}...",
2022-01-31 12:01:11 +00:00
fill="black",
font=font2,
)
draw.text(
2022-02-03 13:57:42 +00:00
(27, 543),
2022-02-01 03:58:59 +00:00
f"Playing on {ctitle[:12]}",
2022-01-31 12:01:11 +00:00
fill="black",
font=font,
)
img.save(f"search/final{userid}.png")
os.remove(f"search/temp{userid}.png")
2022-02-13 08:22:10 +00:00
os.remove(img_path)
2022-01-31 12:01:11 +00:00
final = f"search/final{userid}.png"
return final