Crop only non-cropped

This commit is contained in:
henriquenakashima 2023-06-27 22:56:41 -04:00
parent ca8d110396
commit a962453058

View file

@ -1,6 +1,6 @@
import os
from pathlib import Path
from typing import List
from typing import List, Optional
from PIL import Image, ImageDraw
@ -13,29 +13,49 @@ CORNER_RADIUS_INCHES = 0.12
BASE_DIR = Path(__file__).parent
def gather_input_cards() -> List[str]:
input_path = BASE_DIR / 'input_cards'
def gather_images(directory: Path, midfix : Optional[str] = None) -> List[Path]:
files_to_crop = []
for f in os.listdir(input_path):
possible_image_path = input_path / f
for f in os.listdir(directory):
possible_image_path = directory / f
if not possible_image_path.is_file():
# Skip directory
continue
if any(s == '.crop' for s in possible_image_path.suffixes):
# Skip already cropped
continue
files_to_crop.append(possible_image_path.name)
num_files_to_crop = len(files_to_crop)
print(f'Found {num_files_to_crop} images to crop')
# TODO: Check if is image
if midfix is None or any(s == f'.{midfix}' for s in possible_image_path.suffixes):
files_to_crop.append(possible_image_path)
return files_to_crop
def gather_input_cards() -> List[Path]:
return gather_images(directory=BASE_DIR / 'input_cards', midfix=None)
def gather_cropped_cards() -> List[Path]:
return gather_images(directory=BASE_DIR / 'cropped_cards', midfix='crop')
def get_inputs_without_crop(all_inputs: List[Path], already_cropped: List[Path]) -> List[Path]:
already_cropped_set = set(already_cropped)
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:
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')
def crop(input_filepath: Path) -> Path:
out_dir = BASE_DIR / 'cropped_cards'
out_dir.mkdir(parents=True, exist_ok=True)
cropped_filepath = out_dir / (input_filepath.stem + ".crop" + input_filepath.suffix)
cropped_filepath = input_to_crop_filepath(input_filepath)
try:
with Image.open(input_filepath) as im:
# print(im.info)
@ -90,15 +110,11 @@ def crop(input_filepath: Path) -> Path:
return None
def crop_all_inputs(files_to_crop: List[str]) -> List[Path]:
cropped_filepaths = []
def crop_all_inputs(files_to_crop: List[Path]):
num_files_to_crop = len(files_to_crop)
for i, input_filename in enumerate(files_to_crop, 1):
print(f'[{i}/{num_files_to_crop}] Cropping {input_filename}')
input_filepath = BASE_DIR / 'input_cards' / input_filename
cropped_filepath = crop(input_filepath)
cropped_filepaths.append(cropped_filepath)
return cropped_filepaths
for i, input_filepath in enumerate(files_to_crop, 1):
print(f'[{i}/{num_files_to_crop}] Cropping {input_filepath}')
crop(input_filepath)
def create_sheet(cropped_filepaths: List[Path]) -> Path:
@ -146,11 +162,24 @@ def create_sheet(cropped_filepaths: List[Path]) -> Path:
def main():
all_input_cards = gather_input_cards()
print(f'Found {len(all_input_cards)} input cards:')
print(all_input_cards)
all_cropped_cards = crop_all_inputs(all_input_cards)
already_cropped_cards = gather_cropped_cards()
print(f'Found {len(already_cropped_cards)} already cropped cards:')
print(already_cropped_cards)
cards = all_cropped_cards[:8]
new_inputs_to_crop = get_inputs_without_crop(all_input_cards, already_cropped_cards)
print(f'Found {len(new_inputs_to_crop)} new cards to crop:')
print(new_inputs_to_crop)
crop_all_inputs(new_inputs_to_crop)
all_cropped_cards = gather_cropped_cards()
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',
@ -162,8 +191,8 @@ def main():
# 'Visionary Augmenter.crop.png',
# ]
sheet_filepath = create_sheet(cards)
print(f'Output {sheet_filepath}')
first_sheet_filepath = create_sheet(cards_in_first_sheet)
print(f'Output {first_sheet_filepath}')
if __name__ == '__main__':