Put all cards in sheets

This commit is contained in:
henriquenakashima 2023-06-29 14:25:24 -04:00
parent a962453058
commit 5003c625ba
2 changed files with 34 additions and 27 deletions

View file

@ -4,6 +4,8 @@ from typing import List, Optional
from PIL import Image, ImageDraw from PIL import Image, ImageDraw
import util
W_INCHES_EXPECTED = 2.72 W_INCHES_EXPECTED = 2.72
H_INCHES_EXPECTED = 3.7 H_INCHES_EXPECTED = 3.7
BLEED_INCHES_EXPECTED = 0.12 BLEED_INCHES_EXPECTED = 0.12
@ -39,17 +41,14 @@ def get_inputs_without_crop(all_inputs: List[Path], already_cropped: List[Path])
inputs_without_crop = [] inputs_without_crop = []
for input_filepath in all_inputs: for input_filepath in all_inputs:
possible_cropped_filepath = input_to_crop_filepath(input_filepath) possible_cropped_filepath = input_to_crop_filepath(input_filepath)
if possible_cropped_filepath.name not in already_cropped_set: if possible_cropped_filepath not in already_cropped_set:
inputs_without_crop.append(input_filepath) inputs_without_crop.append(input_filepath)
return inputs_without_crop return inputs_without_crop
def add_midfix(filepath: Path, midfix: str) -> Path:
return filepath.with_stem(f"{filepath.stem}.{midfix}")
def input_to_crop_filepath(input_filepath: Path) -> Path: def input_to_crop_filepath(input_filepath: Path) -> Path:
return add_midfix(input_filepath, 'crop') input_filename = input_filepath.stem + '.crop' + input_filepath.suffix
return BASE_DIR / 'cropped_cards' / input_filename
def crop(input_filepath: Path) -> Path: def crop(input_filepath: Path) -> Path:
@ -117,7 +116,7 @@ def crop_all_inputs(files_to_crop: List[Path]):
crop(input_filepath) crop(input_filepath)
def create_sheet(cropped_filepaths: List[Path]) -> Path: def create_sheet(cropped_filepaths: List[Path], sheet_filepath: Path):
# Create sheet # Create sheet
LETTER_PAGE_W_INCHES = 11 LETTER_PAGE_W_INCHES = 11
LETTER_PAGE_H_INCHES = 8.5 LETTER_PAGE_H_INCHES = 8.5
@ -142,22 +141,21 @@ def create_sheet(cropped_filepaths: List[Path]) -> Path:
sheet = Image.new('RGB', (PAGE_W, PAGE_H), 'white') sheet = Image.new('RGB', (PAGE_W, PAGE_H), 'white')
# Draw cards # Draw cards
assert len(cropped_filepaths) == 8
filenames_iter = cropped_filepaths.__iter__() filenames_iter = cropped_filepaths.__iter__()
for y in range(CARD_ROWS): for y in range(CARD_ROWS):
for x in range(CARD_COLUMNS): for x in range(CARD_COLUMNS):
filepath = filenames_iter.__next__() try:
filepath = filenames_iter.__next__()
except StopIteration:
break
with Image.open(filepath) as card_im: with Image.open(filepath) as card_im:
card_im = card_im.resize((CROPPED_IMAGE_DEST_W, CROPPED_IMAGE_DEST_H), resample = Image.Resampling.LANCZOS) card_im = card_im.resize((CROPPED_IMAGE_DEST_W, CROPPED_IMAGE_DEST_H), resample = Image.Resampling.LANCZOS)
sheet.paste(card_im, (BLANK_SPACE_W + (CROPPED_IMAGE_DEST_W + SPACING) * x, BLANK_SPACE_H + (CROPPED_IMAGE_DEST_H + SPACING) * y)) sheet.paste(card_im, (BLANK_SPACE_W + (CROPPED_IMAGE_DEST_W + SPACING) * x, BLANK_SPACE_H + (CROPPED_IMAGE_DEST_H + SPACING) * y))
# Save sheet # Save sheet
out_dir = BASE_DIR / 'sheets' sheet.save(sheet_filepath, 'JPEG', dpi=(DPI, DPI), quality=100, subsampling=0)
out_dir.mkdir(parents=True, exist_ok=True)
outfile = out_dir / 'sheet.jpg'
sheet.save(outfile, 'JPEG', dpi=(DPI, DPI), quality=100, subsampling=0)
return outfile
def main(): def main():
@ -179,20 +177,23 @@ def main():
print(f'Now there are {len(already_cropped_cards)} cropped cards:') print(f'Now there are {len(already_cropped_cards)} cropped cards:')
print(already_cropped_cards) print(already_cropped_cards)
cards_in_first_sheet = all_cropped_cards[:8] sheets_dir = BASE_DIR / 'sheets'
# cards = [ sheets_dir.mkdir(parents=True, exist_ok=True)
# 'Svella, Ice Shaper (Extended Andrew Mar).crop.jpg', for i, cards_in_sheet in enumerate(util.batched(all_cropped_cards, 8), 1):
# 'Icon of Ancestry.crop.png', # cards_in_sheet = [
# 'Condescend.crop.png', # 'Svella, Ice Shaper (Extended Andrew Mar).crop.jpg',
# 'Sulfur Falls (Cliff Childs).crop.png', # 'Icon of Ancestry.crop.png',
# 'Giant Killer.crop.png', # 'Condescend.crop.png',
# 'Magda, Brazen Outlaw.crop.png', # 'Sulfur Falls (Cliff Childs).crop.png',
# 'Rhox.crop.png', # 'Giant Killer.crop.png',
# 'Visionary Augmenter.crop.png', # 'Magda, Brazen Outlaw.crop.png',
# ] # 'Rhox.crop.png',
# 'Visionary Augmenter.crop.png',
# ]
first_sheet_filepath = create_sheet(cards_in_first_sheet) sheet_filepath = sheets_dir / f'sheet{i:03d}.jpg'
print(f'Output {first_sheet_filepath}') create_sheet(cards_in_sheet, sheet_filepath)
print(f'Output {sheet_filepath}')
if __name__ == '__main__': if __name__ == '__main__':

6
util.py Normal file
View file

@ -0,0 +1,6 @@
import itertools
def batched(iterable, n):
it = iter(iterable)
while batch := tuple(itertools.islice(it, n)):
yield batch