2021-08-23 12:56:30 +00:00
|
|
|
import logging
|
2021-08-23 14:00:17 +00:00
|
|
|
import os
|
2021-09-01 17:01:53 +00:00
|
|
|
from dataclasses import replace
|
2021-08-04 16:36:23 +00:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
2021-08-09 05:01:25 +00:00
|
|
|
from .cards import Cards
|
|
|
|
from .imageloader import ImageLoader
|
2021-08-23 16:16:56 +00:00
|
|
|
from .language import Language
|
2021-08-25 14:13:07 +00:00
|
|
|
from .utils import GRID, RESOLUTION, CARD_BACK_URL, IMAGES_DIR_NAME, chunks, grid_paste
|
2021-08-04 16:36:23 +00:00
|
|
|
|
|
|
|
|
2021-08-04 01:39:19 +00:00
|
|
|
class Book:
|
2021-08-23 16:16:56 +00:00
|
|
|
def __init__(self, cards: Cards, language: Language, num_threads: int):
|
2021-08-04 16:36:23 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-08-04 22:17:47 +00:00
|
|
|
# all card face URLs
|
2021-08-20 22:16:38 +00:00
|
|
|
urls = [
|
2021-08-25 14:13:07 +00:00
|
|
|
("https://fftcg.cdn.sewest.net/images/cards/full/{}_{}.jpg", card.code.long, language.image_suffix)
|
2021-08-20 22:16:38 +00:00
|
|
|
for card in cards
|
|
|
|
]
|
2021-08-18 11:49:34 +00:00
|
|
|
# card back URL
|
2021-09-03 07:29:40 +00:00
|
|
|
urls.insert(0, (CARD_BACK_URL, "", ""))
|
2021-08-04 22:17:47 +00:00
|
|
|
|
2021-08-09 12:03:45 +00:00
|
|
|
# multi-threaded download
|
2021-09-03 07:29:40 +00:00
|
|
|
images = iter(ImageLoader.load(urls, num_threads))
|
2021-08-04 22:17:47 +00:00
|
|
|
# card back Image
|
2021-09-03 07:29:40 +00:00
|
|
|
back_image = next(images)
|
2021-08-04 01:39:19 +00:00
|
|
|
|
2021-08-04 16:36:23 +00:00
|
|
|
self.__pages = []
|
2021-08-18 14:52:32 +00:00
|
|
|
|
2021-08-25 14:13:07 +00:00
|
|
|
for page_num, (page_images, page_cards) in enumerate(zip(
|
|
|
|
chunks(GRID.capacity, images), chunks(GRID.capacity, cards)
|
|
|
|
)):
|
2021-09-01 17:01:53 +00:00
|
|
|
file_name = f"{cards.file_name}_{page_num}.jpg"
|
|
|
|
|
2021-08-04 22:17:47 +00:00
|
|
|
# create book page Image
|
2021-08-17 15:37:28 +00:00
|
|
|
page_image = Image.new("RGB", GRID * RESOLUTION)
|
2021-08-09 12:03:45 +00:00
|
|
|
logger.info(f"New image: {page_image.size[0]}x{page_image.size[1]}")
|
2021-08-04 16:36:23 +00:00
|
|
|
|
2021-08-04 22:17:47 +00:00
|
|
|
# paste card faces onto page
|
2021-08-09 12:03:45 +00:00
|
|
|
for i, image in enumerate(page_images):
|
2021-08-25 14:13:07 +00:00
|
|
|
grid_paste(page_image, i, image)
|
2021-08-04 22:17:47 +00:00
|
|
|
|
|
|
|
# paste card back in last position
|
2021-08-25 14:13:07 +00:00
|
|
|
grid_paste(page_image, GRID.capacity, back_image)
|
2021-08-09 12:03:45 +00:00
|
|
|
|
2021-08-18 14:52:32 +00:00
|
|
|
# set card indices
|
|
|
|
for i, card in enumerate(page_cards):
|
|
|
|
card.index = i
|
2021-09-01 17:01:53 +00:00
|
|
|
card[language] = replace(card[language], face=file_name)
|
2021-08-18 14:52:32 +00:00
|
|
|
|
2021-08-09 12:03:45 +00:00
|
|
|
# save page
|
|
|
|
self.__pages.append({
|
2021-09-01 17:01:53 +00:00
|
|
|
"file_name": file_name,
|
2021-08-09 12:03:45 +00:00
|
|
|
"image": page_image,
|
|
|
|
"cards": page_cards,
|
|
|
|
})
|
2021-08-04 16:36:23 +00:00
|
|
|
|
2021-08-18 14:52:32 +00:00
|
|
|
def save(self) -> None:
|
2021-08-23 14:00:17 +00:00
|
|
|
if not os.path.exists(IMAGES_DIR_NAME):
|
|
|
|
os.mkdir(IMAGES_DIR_NAME)
|
|
|
|
|
2021-08-18 14:52:32 +00:00
|
|
|
# save images
|
2021-08-23 14:46:53 +00:00
|
|
|
for page in self.__pages:
|
2021-09-03 07:29:40 +00:00
|
|
|
page["image"].save(
|
|
|
|
os.path.join(IMAGES_DIR_NAME, page["file_name"])
|
|
|
|
)
|