-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_util.py
44 lines (35 loc) · 1.16 KB
/
render_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import tempfile
from wiki_api import USER_AGENT
import gtts
import moviepy.editor as mpy
import requests
import PIL.ImageFile
import PIL.Image
import temp
# HACK: Tell PIL to just load slightly damaged images
PIL.ImageFile.LOAD_TRUNCATED_IMAGES = True
def tts_speak(text: str) -> mpy.AudioFileClip:
"""
Uses Google TTS to speak `text`, and then converts that sound
into a MoviePy AudioFileClip.
"""
tts = gtts.gTTS(text)
with temp.get_temp_file() as temp_file:
tts.write_to_fp(temp_file)
clip = mpy.AudioFileClip(temp_file.name)
return clip
def image_download(url: str):
"""
Downloads the image at URL and saves it into a
ImageClip.
"""
response = requests.get(url, headers={"User-Agent": USER_AGENT})
with temp.get_temp_file() as temp_file:
temp_file.write(response.content)
# add a background to images that don't have one
image = PIL.Image.open(temp_file.name).convert("RGBA")
bg = PIL.Image.new("RGBA", image.size, (255, 255, 255, 255))
with_background = PIL.Image.alpha_composite(bg, image).save(
temp_file.name, format="png"
)
return mpy.ImageClip(temp_file.name)