1
0
Fork 0
mirror of https://github.com/ldericher/fftcgtool synced 2025-01-15 15:02:59 +00:00
fftcgtool/fftcg/book.py
2021-08-04 20:15:38 +02:00

42 lines
1.1 KiB
Python

import logging
from PIL import Image
from .imageloader import ImageLoader
def chunks(whole: list, chunk_size):
# while there are elements
while whole:
# get a chunk
yield whole[:chunk_size]
# remove that chunk
whole = whole[chunk_size:]
class Book:
def __init__(self, cards, grid, resolution, language, num_threads):
logger = logging.getLogger(__name__)
images = ImageLoader.load(cards, resolution, language, num_threads)
# shorthands
# rows and columns per sheet
r, c = grid
# width, height per card
w, h = resolution
self.__pages = []
for images in chunks(images, r * c - 1):
page = Image.new("RGB", (c * w, r * h))
logger.info(f"New image: {page.size[0]}x{page.size[1]}")
for i, image in enumerate(images):
x, y = (i % c) * w, (i // c) * h
page.paste(image, (x, y))
self.__pages.append(page)
def save(self, filename):
for i, page in enumerate(self.__pages):
page.save(filename.format(i))