For overlaying images, cv2 is significantly more efficient. I wrote a basic test to compare the two, and the result were astounding. To write 100 frames, pillow took ~20.3 seconds while cv2 took ~4.3 seconds, meaning that swapping will significantly speed up frame generation times by up to 5x
My test code:
import time
import cv2
from PIL import Image
n = 100
# PIL
start = time.time()
for i in range(n):
face = Image.open('base.png').convert('RGBA')
mouth = Image.open('hat.png').convert('RGBA')
face.paste(mouth, (0, 0), mouth)
face.save(f'pil/{i}.png')
end = time.time()
print(f'PIL: {end-start}')
start = time.time()
for i in range(n):
face = cv2.imread("base.png")
mouth = cv2.imread("hat.png")
indentY = 0
indentX = 0
height, width, depth = mouth.shape
face[indentY: indentY + height, indentX: indentX + width ] = mouth
cv2.imwrite(f'cv2/{i}.png', face)
end = time.time()
print(f'cv2: {end - start}')
For overlaying images, cv2 is significantly more efficient. I wrote a basic test to compare the two, and the result were astounding. To write 100 frames, pillow took ~20.3 seconds while cv2 took ~4.3 seconds, meaning that swapping will significantly speed up frame generation times by up to 5x
My test code: