Source code for wed.chaptors.title_call

# coding: utf-8
"""This submodule contains a set of functions for editing the following image:

.. image:: _images/chaptors/marquee.png
   :class: popup-img

"""
from typing import Any, Dict, List, Optional, Tuple, Union

import numpy as np
import numpy.typing as npt
import scipy as sp
import scipy.ndimage
from PIL import Image

from ..utils._colorings import toBLUE, toGREEN
from ..utils._fonts import FONT_MODI_MEMOIR_PATH
from ..utils.generic_utils import Cycler, handleKeyError
from ..utils.image_utils import arr2pil, draw_text_in_pil, pil2arr
from .base_editor import BaseWedOPEditor


[docs]class TitleCallEditor(BaseWedOPEditor): POSITIONS = { "upper": { "fontsize": 50, "positions": [ (195, 120), (295, 105), (390, 120), ], "timings": [400, 410, 420], }, "middle": {"fontsize": 20, "positions": [(310, 162)], "timings": [430]}, "lower": { "fontsize": 50, "positions": [ (130, 200), (195, 200), (260, 200), (330, 200), (400, 200), (465, 200), ], "timings": [440, 443, 447, 450, 456, 460], }, } def __init__( self, upper_text: str = "水曜日", middle_text: str = "の", lower_text: str = "ダウンタウン", ttfontname: str = FONT_MODI_MEMOIR_PATH, expansion_time: int = 10, ): super().__init__( positions=(375, 492), ) self.upper_text = upper_text self.middle_text = middle_text self.lower_text = lower_text self.ttfontname = ttfontname self.expansion_time = expansion_time
[docs] def draw_text(self, text, xy=(0, 0), fontsize=10): img = self.get_frame(pos=380, as_pil=True) img, _ = draw_text_in_pil( text=text, xy=xy, ttfontname=self.ttfontname, fontsize=fontsize, img=img ) return img
[docs] def edit( self, frame: npt.NDArray[np.uint8], pos: int, span: int = 20 ) -> npt.NDArray[np.uint8]: """Edit the image if it is an assigned chapter (``pos``) Args: frame (npt.NDArray[np.uint8]) : Current frame (BGR image) in the video. pos (int) : Current position in the video. Returns: npt.NDArray[np.uint8]: Edited frame. """ frame = self.bounce_text(frame=frame, pos=pos) return frame
[docs] def bounce_text( self, frame: npt.NDArray[np.uint8], pos: int, **kwargs, ) -> npt.NDArray[np.uint8]: img = arr2pil(frame) for place, values in TitleCallEditor.POSITIONS.items(): text = getattr(self, f"{place}_text") fontsize = values["fontsize"] positions = values["positions"] timings = values["timings"] for t, (x, y), timings in zip(text, positions, timings): if timings < pos: time_lag = min(self.expansion_time, pos - timings) fs = fontsize * time_lag / self.expansion_time x += fontsize - fs y += fontsize - fs img, _ = draw_text_in_pil( text=t, ttfontname=self.ttfontname, img=img, xy=(x, y), fontsize=fs, ) frame = pil2arr(img) return frame