From 7d5b4b0fcf81ae386a636e56ff0c2f0b5b0548ef Mon Sep 17 00:00:00 2001 From: henriquenakashima Date: Sat, 1 Jul 2023 01:19:58 -0400 Subject: [PATCH] Update crop_card.py --- crop_card.py | 62 +++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/crop_card.py b/crop_card.py index c3dcff3..3a3057b 100644 --- a/crop_card.py +++ b/crop_card.py @@ -36,6 +36,9 @@ def gather_cropped_cards() -> List[Path]: def get_inputs_without_crop(all_inputs: List[Path], already_cropped: List[Path]) -> List[Path]: + # For debug, always crop + return all_inputs + already_cropped_set = set(already_cropped) inputs_without_crop = [] for input_filepath in all_inputs: @@ -50,6 +53,37 @@ def input_to_crop_filepath(input_filepath: Path) -> Path: return BASE_DIR / 'cropped_cards' / input_filename +def add_cut_guides(im: Image, dpi: float): + cropped_w, cropped_h = im.width, im.height + cut_pixels = LEAVE_BLEED_INCHES * dpi + cut_pixels_int = round(cut_pixels) + draw = ImageDraw.Draw(im) + LENGTH_CUT_GUIDE_INCHES = 0.06 + CUT_GUIDE_WIDTH_INCHES = 0.006 + guide_len = round(LENGTH_CUT_GUIDE_INCHES * dpi) + guide_thickness = round(CUT_GUIDE_WIDTH_INCHES * dpi) + cut_x_l = cut_pixels_int + cut_x_r = cropped_w - cut_pixels_int + cut_y_t = cut_pixels_int + cut_y_b = cropped_h - cut_pixels_int + + color = 'magenta' + + # Top left + draw.line([(cut_x_l, cut_y_t), (cut_x_l, cut_y_t + guide_len)], fill = color, width = guide_thickness) + draw.line([(cut_x_l, cut_y_t), (cut_x_l + guide_len, cut_y_t)], fill = color, width = guide_thickness) + # Bottom left + draw.line([(cut_x_l, cut_y_b), (cut_x_l, cut_y_b - guide_len)], fill = color, width = guide_thickness) + draw.line([(cut_x_l, cut_y_b), (cut_x_l + guide_len, cut_y_b)], fill = color, width = guide_thickness) + # Top right + draw.line([(cut_x_r, cut_y_t), (cut_x_r, cut_y_t + guide_len)], fill = color, width = guide_thickness) + draw.line([(cut_x_r, cut_y_t), (cut_x_r - guide_len, cut_y_t)], fill = color, width = guide_thickness) + # Bottom right + draw.line([(cut_x_r, cut_y_b), (cut_x_r, cut_y_b - guide_len)], fill = color, width = guide_thickness) + draw.line([(cut_x_r, cut_y_b), (cut_x_r - guide_len, cut_y_b)], fill = color, width = guide_thickness) + + + def crop(input_filepath: Path) -> Path: out_dir = BASE_DIR / 'cropped_cards' out_dir.mkdir(parents=True, exist_ok=True) @@ -71,36 +105,10 @@ def crop(input_filepath: Path) -> Path: # Crop crop_box = (crop_pixels_int, crop_pixels_int, w - crop_pixels_int, h - crop_pixels_int) cropped_part = rgb_im.crop(crop_box) - cropped_w, cropped_h = cropped_part.width, cropped_part.height # Draw where cuts should go # TODO: Move this after composing a sheet and add guide lines to the edge - cut_pixels = LEAVE_BLEED_INCHES * dpi_by_w - cut_pixels_int = round(cut_pixels) - draw = ImageDraw.Draw(cropped_part) - LENGTH_CUT_GUIDE_INCHES = 0.06 - CUT_GUIDE_WIDTH_INCHES = 0.006 - guide_len = round(LENGTH_CUT_GUIDE_INCHES * dpi_by_w) - guide_thickness = round(CUT_GUIDE_WIDTH_INCHES * dpi_by_w) - cut_x_l = cut_pixels_int - cut_x_r = cropped_w - cut_pixels_int - cut_y_t = cut_pixels_int - cut_y_b = cropped_h - cut_pixels_int - - color = 'magenta' - - # Top left - draw.line([(cut_x_l, cut_y_t), (cut_x_l, cut_y_t + guide_len)], fill = color, width = guide_thickness) - draw.line([(cut_x_l, cut_y_t), (cut_x_l + guide_len, cut_y_t)], fill = color, width = guide_thickness) - # Bottom left - draw.line([(cut_x_l, cut_y_b), (cut_x_l, cut_y_b - guide_len)], fill = color, width = guide_thickness) - draw.line([(cut_x_l, cut_y_b), (cut_x_l + guide_len, cut_y_b)], fill = color, width = guide_thickness) - # Top right - draw.line([(cut_x_r, cut_y_t), (cut_x_r, cut_y_t + guide_len)], fill = color, width = guide_thickness) - draw.line([(cut_x_r, cut_y_t), (cut_x_r - guide_len, cut_y_t)], fill = color, width = guide_thickness) - # Bottom right - draw.line([(cut_x_r, cut_y_b), (cut_x_r, cut_y_b - guide_len)], fill = color, width = guide_thickness) - draw.line([(cut_x_r, cut_y_b), (cut_x_r - guide_len, cut_y_b)], fill = color, width = guide_thickness) + add_cut_guides(cropped_part, dpi_by_w) cropped_part.save(cropped_filepath, 'JPEG', quality=100, subsampling=0) return cropped_filepath