2021-08-17 15:37:28 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-08-09 12:03:45 +00:00
|
|
|
from PIL import Image
|
|
|
|
|
2021-08-10 01:26:35 +00:00
|
|
|
_Point = tuple[int, int]
|
2021-08-09 12:03:45 +00:00
|
|
|
|
2021-08-10 01:26:35 +00:00
|
|
|
|
2021-08-17 15:37:28 +00:00
|
|
|
class Grid(tuple[int, int]):
|
|
|
|
def __mul__(self, other: Grid) -> Grid:
|
|
|
|
return Grid((self.x * other.x, self.y * other.y))
|
2021-08-09 12:03:45 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def x(self):
|
|
|
|
return self[0]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def y(self):
|
|
|
|
return self[1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def capacity(self):
|
|
|
|
# capacity of grid (reserve last space for card back)
|
|
|
|
return self.x * self.y - 1
|
|
|
|
|
|
|
|
def chunks(self, whole: list) -> list:
|
|
|
|
# while there are elements
|
|
|
|
while whole:
|
|
|
|
# get a chunk
|
|
|
|
yield whole[:self.capacity]
|
|
|
|
# remove that chunk
|
|
|
|
whole = whole[self.capacity:]
|
|
|
|
|
|
|
|
def paste(self, page: Image.Image, index: int, card: Image.Image) -> None:
|
|
|
|
w, h = card.size
|
|
|
|
position = (index % self.x) * w, (index // self.x) * h
|
|
|
|
page.paste(card, position)
|