comixify/comic_layout/comic_layout.py

34 lines
1.1 KiB
Python
Raw Permalink Normal View History

2018-07-31 19:51:39 +00:00
import cv2
2018-08-20 23:00:22 +00:00
import numpy as np
from utils import profile
2018-07-31 19:51:39 +00:00
class LayoutGenerator():
2018-08-20 23:00:22 +00:00
@classmethod
@profile
2018-08-20 23:00:22 +00:00
def get_layout(cls, frames):
result_imgs = cls._pad_images(frames)
first_row = np.hstack(result_imgs[:2])
second_row = np.hstack(result_imgs[2:5])
third_row = np.hstack(result_imgs[5:7])
fourth_row = np.hstack(result_imgs[7:10])
second_row = cv2.resize(second_row,
(first_row.shape[1],
2018-08-22 09:52:17 +00:00
(second_row.shape[0] * first_row.shape[1]) // second_row.shape[1]))
2018-08-20 23:00:22 +00:00
fourth_row = cv2.resize(fourth_row,
(first_row.shape[1],
2018-08-22 09:52:17 +00:00
(fourth_row.shape[0] * first_row.shape[1]) // fourth_row.shape[1]))
2018-08-20 23:00:22 +00:00
return np.vstack([first_row, second_row, third_row, fourth_row])
2018-07-31 19:51:39 +00:00
@staticmethod
2018-08-20 23:00:22 +00:00
def _pad_images(frames):
padded_result_imgs = []
for img in frames:
padded_img = cv2.copyMakeBorder(img, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=(255, 255, 255))
2018-08-20 23:00:22 +00:00
padded_result_imgs.append(padded_img)
return padded_result_imgs