图像处理基础 - PIL & moviepy & TTS
PIL
from PIL import Image
打开图像
img = Image.open(path)
基本变换
img_resize = img.resize((400, 400), Image.Resampling.LANCZOS)
img_gray = img.convert('L')
img_rotate = img.rotate(45, expand=True)
LANCZOS
插值使用 Lanczos kernel {\displaystyle L(x)={\begin{cases}\operatorname {sinc} (x)\operatorname {sinc} (x/a)&{\text{if}}\ -a<x<a,\\0&{\text{otherwise}}.\end{cases}}}
{\displaystyle L(x,y)=L(x)L(y)}对图片进行插值
expand=True
时增广的部分填充 (0, 0, 0)
卷积
锐化核 \displaystyle\frac{1}{9}\begin{bmatrix} -1 & -1 & -1 \\ -1 & 9 & -1 \\ -1 & -1 & -1 \end{bmatrix}作用
from PIL import Image, ImageFilter
img_sharpen = img.filter(ImageFilter.Kernal((3, 3), kernel.flatten())
numpy
import numpy as np
img_arr = np.array(img)
img_arr[:, :, 1] = 0
img_nogreen = Image.fromarray(img_arr)
混合
img_blend = Image.blend(img, img_nogreen, alpha=0.7)
混合方法是 \text{img1} \cdot (1 - \alpha) + \text{img2} \cdot \alpha
生成 GIF
frames = [img.rotate(i * 12, expand=True) for i in range(30)]
frames[0].save(path, save_all=True, append_images=frames[1:], loop=0, duration=60)
指定每帧播放 60 ms
moviepy
import moviepy.editor as mpy
img_seq = [mpy.ImageClip(img).set_duration(1/fps) for img in frames]
video = mpy.concatenate_videoclips(img_seq, method='compose')
audio = AudioFileClip(path)
笔者吐槽:用 Python 剪辑视频完全是意义不明的事情...
TTS
import pyttsx3
engine = pyttsx3.init()
engine.setProperty('rate', 180)
engine.save_to_file(text, path)
engine.runAndWait()
图像处理基础 - PIL & moviepy & TTS
http://localhost:8090/archives/RiEeaHfT