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

68 lines
2.1 KiB
Python
Raw Normal View History

2021-08-23 12:56:30 +00:00
import logging
2021-08-23 14:00:17 +00:00
import os
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
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__)
# all card face URLs
2021-08-20 22:16:38 +00:00
urls = [
("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
urls.append((CARD_BACK_URL, "", ""))
2021-08-09 12:03:45 +00:00
# multi-threaded download
2021-08-20 22:16:38 +00:00
images = ImageLoader.load(urls, num_threads)
# card back Image
back_image = images.pop(-1)
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
for page_num, (page_images, page_cards) in enumerate(zip(
chunks(GRID.capacity, images), chunks(GRID.capacity, cards)
)):
file_name = f"{cards.file_name}_{page_num}.jpg"
# 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
# paste card faces onto page
2021-08-09 12:03:45 +00:00
for i, image in enumerate(page_images):
grid_paste(page_image, i, image)
# paste card back in last position
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
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({
"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:
page["file_name"] = os.path.join(IMAGES_DIR_NAME, page["file_name"])
page["image"].save(page["file_name"])