video-stream/driver/design/thumbnail.py

55 lines
1.8 KiB
Python
Raw Normal View History

2021-12-23 21:56:00 +00:00
import os
import aiofiles
import aiohttp
from PIL import Image, ImageDraw, ImageFont
def changeImageSize(maxWidth, maxHeight, image):
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))
return newImage
async def thumb(thumbnail, title, userid, ctitle):
async with aiohttp.ClientSession() as session:
async with session.get(thumbnail) as resp:
if resp.status == 200:
f = await aiofiles.open(f"search/thumb{userid}.png", mode="wb")
await f.write(await resp.read())
await f.close()
image1 = Image.open(f"search/thumb{userid}.png")
2021-12-24 03:55:09 +00:00
image2 = Image.open("driver/source/LightBlue.png")
2021-12-23 21:56:00 +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)
2021-12-24 03:55:09 +00:00
font = ImageFont.truetype("driver/source/regular.ttf", 52)
font2 = ImageFont.truetype("driver/source/medium.ttf", 76)
2021-12-23 21:56:00 +00:00
draw.text(
2021-12-24 03:57:25 +00:00
(25, 610),
2021-12-24 03:55:09 +00:00
f"{title[:18]}...",
2021-12-23 21:56:00 +00:00
fill="White",
stroke_width=1,
stroke_fill="black",
font=font2,
)
draw.text(
2021-12-24 03:57:25 +00:00
(27, 535),
2021-12-24 03:55:09 +00:00
f"Playing on {ctitle[:8]}...",
2021-12-23 21:56:00 +00:00
fill="White",
stroke_width=1,
stroke_fill="black",
font=font,
)
img.save(f"search/final{userid}.png")
os.remove(f"search/temp{userid}.png")
os.remove(f"search/thumb{userid}.png")
final = f"search/final{userid}.png"
return final