46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
import asyncio
|
|
from typing import Any, cast
|
|
|
|
import cv2
|
|
import numpy as np
|
|
import PIL.Image
|
|
|
|
from konabot.common.path import ASSETS_PATH
|
|
|
|
cao_image = PIL.Image.open(ASSETS_PATH / "img" / "meme" / "caoimg1.png")
|
|
CAO_QUAD_POINTS = np.float32(cast(Any, [
|
|
[392, 540],
|
|
[577, 557],
|
|
[567, 707],
|
|
[381, 687],
|
|
]))
|
|
|
|
def _draw_cao_display(image: PIL.Image.Image):
|
|
src = np.array(image.convert("RGB"))
|
|
h, w = src.shape[:2]
|
|
src_points = np.float32(cast(Any, [
|
|
[0, 0],
|
|
[w, 0],
|
|
[w, h],
|
|
[0, h]
|
|
]))
|
|
dst_points = CAO_QUAD_POINTS
|
|
M = cv2.getPerspectiveTransform(cast(Any, src_points), cast(Any, dst_points))
|
|
output_size = cao_image.size
|
|
output_w, output_h = output_size
|
|
warped = cv2.warpPerspective(
|
|
src,
|
|
M,
|
|
(output_w, output_h),
|
|
flags=cv2.INTER_LINEAR,
|
|
borderMode=cv2.BORDER_CONSTANT,
|
|
borderValue=(0, 0, 0)
|
|
)
|
|
result = PIL.Image.fromarray(warped, 'RGB').convert('RGBA')
|
|
result = PIL.Image.alpha_composite(result, cao_image)
|
|
return result
|
|
|
|
|
|
async def draw_cao_display(image: PIL.Image.Image):
|
|
return await asyncio.to_thread(_draw_cao_display, image)
|