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

95 lines
3 KiB
Python
Raw Normal View History

2021-08-04 16:36:23 +00:00
import logging
2021-08-16 01:46:27 +00:00
import yaml
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 .code import Code
2021-08-09 12:03:45 +00:00
from .grid import Grid
2021-08-09 05:01:25 +00:00
from .imageloader import ImageLoader
2021-08-04 16:36:23 +00:00
2021-08-04 01:39:19 +00:00
class Book:
2021-08-09 03:32:05 +00:00
def __init__(self, cards: Cards, grid: tuple[int, int], resolution: tuple[int, int], language: str,
2021-08-09 02:03:24 +00:00
num_threads: int):
2021-08-04 16:36:23 +00:00
logger = logging.getLogger(__name__)
2021-08-09 12:03:45 +00:00
# transform grid into Grid
grid = Grid(grid)
# sort cards by element, then alphabetically
cards.sort(key=lambda x: x.name)
cards.sort(key=lambda x: "Multi" if len(x.elements) > 1 else x.elements[0])
# all card face URLs
2021-08-09 12:03:45 +00:00
urls = [f"https://fftcg.cdn.sewest.net/images/cards/full/{card.code}_{language}.jpg" for card in cards]
# card back URL (image by Aurik)
urls.append(
"http://cloud-3.steamusercontent.com/ugc/948455238665576576/85063172B8C340602E8D6C783A457122F53F7843/"
)
2021-08-09 12:03:45 +00:00
# multi-threaded download
images = ImageLoader.load(urls, resolution, language, 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-09 12:03:45 +00:00
for page_images, page_cards in zip(grid.chunks(images), grid.chunks(cards)):
# create book page Image
2021-08-09 12:03:45 +00:00
page_image = Image.new("RGB", grid * resolution)
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
2021-08-09 12:03:45 +00:00
grid.paste(page_image, grid.capacity, back_image)
# save page
self.__pages.append({
"image": page_image,
"cards": page_cards,
})
2021-08-04 16:36:23 +00:00
2021-08-09 12:03:45 +00:00
def __getitem__(self, index: int) -> Image.Image:
return self.__pages[index]["image"]
2021-08-04 16:36:23 +00:00
2021-08-09 02:03:24 +00:00
def save(self, filename: str) -> None:
book: dict[str, dict[str, any]]
2021-08-16 01:46:27 +00:00
# load book.yml file
2021-08-16 01:46:27 +00:00
try:
with open("book.yml", "r") as file:
book = yaml.load(file, Loader=yaml.Loader)
2021-08-16 01:46:27 +00:00
except FileNotFoundError:
book = {}
2021-08-16 01:46:27 +00:00
# save book
2021-08-04 16:36:23 +00:00
for i, page in enumerate(self.__pages):
2021-08-16 01:46:27 +00:00
fn = f"{filename}_{i}.jpg"
# save page image
page["image"].save(fn)
# add contents of that image
book[fn] = {"cards": page["cards"]}
# update book.yml file
with open("book.yml", "w") as file:
yaml.dump(book, file, Dumper=yaml.Dumper)
# invert book
inverse_book: dict[Code, dict[str, any]] = {}
for fn, content in book.items():
inverse_book |= {
str(card.code): {
"card": card,
"file": fn,
"index": i
} for i, card in enumerate(content["cards"])
}
# write inverse_book.yml file
with open("inverse_book.yml", "w") as file:
yaml.dump(inverse_book, file, Dumper=yaml.Dumper)