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
import util
W_INCHES_EXPECTED = 2.72
H_INCHES_EXPECTED = 3.7
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 = []
for input_filepath in all_inputs:
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)
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:
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:
@ -117,7 +116,7 @@ def crop_all_inputs(files_to_crop: List[Path]):
crop(input_filepath)
def create_sheet(cropped_filepaths: List[Path]) -> Path:
def create_sheet(cropped_filepaths: List[Path], sheet_filepath: Path):
# Create sheet
LETTER_PAGE_W_INCHES = 11
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')
# Draw cards
assert len(cropped_filepaths) == 8
filenames_iter = cropped_filepaths.__iter__()
for y in range(CARD_ROWS):
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:
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))
# Save sheet
out_dir = BASE_DIR / 'sheets'
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
sheet.save(sheet_filepath, 'JPEG', dpi=(DPI, DPI), quality=100, subsampling=0)
def main():
@ -179,20 +177,23 @@ def main():
print(f'Now there are {len(already_cropped_cards)} cropped cards:')
print(already_cropped_cards)
cards_in_first_sheet = all_cropped_cards[:8]
# cards = [
# 'Svella, Ice Shaper (Extended Andrew Mar).crop.jpg',
# 'Icon of Ancestry.crop.png',
# 'Condescend.crop.png',
# 'Sulfur Falls (Cliff Childs).crop.png',
# 'Giant Killer.crop.png',
# 'Magda, Brazen Outlaw.crop.png',
# 'Rhox.crop.png',
# 'Visionary Augmenter.crop.png',
# ]
sheets_dir = BASE_DIR / 'sheets'
sheets_dir.mkdir(parents=True, exist_ok=True)
for i, cards_in_sheet in enumerate(util.batched(all_cropped_cards, 8), 1):
# cards_in_sheet = [
# 'Svella, Ice Shaper (Extended Andrew Mar).crop.jpg',
# 'Icon of Ancestry.crop.png',
# 'Condescend.crop.png',
# 'Sulfur Falls (Cliff Childs).crop.png',
# 'Giant Killer.crop.png',
# 'Magda, Brazen Outlaw.crop.png',
# 'Rhox.crop.png',
# 'Visionary Augmenter.crop.png',
# ]
first_sheet_filepath = create_sheet(cards_in_first_sheet)
print(f'Output {first_sheet_filepath}')
sheet_filepath = sheets_dir / f'sheet{i:03d}.jpg'
create_sheet(cards_in_sheet, sheet_filepath)
print(f'Output {sheet_filepath}')
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