comixify/comic_layout/comic_layout.py

47 lines
1.3 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-12-10 20:37:53 +00:00
2018-08-20 23:00:22 +00:00
@classmethod
2018-12-10 20:37:53 +00:00
def _gen_image_row(cls, images, width):
image_row = np.hstack(images)
if image_row.shape[1] != width:
nominal_height = image_row.shape[0]
image_row = cv2.resize(
image_row,
(width, (nominal_height * width) // nominal_height)
)
return image_row
@classmethod
def _get_row_layout(cls, scores):
return [2, 3, 1, 3, 1]
2018-08-20 23:00:22 +00:00
2018-12-10 20:37:53 +00:00
@classmethod
@profile
def get_layout(cls, frames, scores):
result_images = cls._pad_images(frames)
row_layout = cls._get_row_layout(scores)
width = result_images[0].shape[1]
2018-08-20 23:00:22 +00:00
2018-12-10 20:37:53 +00:00
index = 0
rows = []
for row_length in row_layout:
row = cls._gen_image_row(result_images[index:index + row_length], width)
rows.append(row)
index += row_length
2018-08-20 23:00:22 +00:00
2018-12-10 20:37:53 +00:00
return np.vstack(rows)
2018-08-20 23:00:22 +00:00
2018-07-31 19:51:39 +00:00
@staticmethod
2018-08-20 23:00:22 +00:00
def _pad_images(frames):
2018-12-10 20:37:53 +00:00
padded_result_images = []
2018-08-20 23:00:22 +00:00
for img in frames:
padded_img = cv2.copyMakeBorder(img, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=(255, 255, 255))
2018-12-10 20:37:53 +00:00
padded_result_images.append(padded_img)
return padded_result_images