Draw crop guides onto sheet

This commit is contained in:
henriquenakashima 2023-07-03 12:28:33 -04:00
parent 9fd1ec6e1e
commit 0e30e652dd

View file

@ -11,6 +11,7 @@ H_INCHES_EXPECTED = 3.7
BLEED_INCHES_EXPECTED = 0.12
LEAVE_BLEED_INCHES = 0.04
CUT_GUIDE_WIDTH_INCHES = 0.006
LENGTH_CUT_GUIDE_INCHES = 0.06
BASE_DIR = Path(__file__).parent
@ -55,36 +56,6 @@ 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
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)
@ -107,10 +78,6 @@ def crop(input_filepath: Path) -> Path:
crop_box = (crop_pixels_int, crop_pixels_int, w - crop_pixels_int, h - crop_pixels_int)
cropped_part = rgb_im.crop(crop_box)
# Draw where cuts should go
# TODO: Move this after composing a sheet and add guide lines to the edge
add_cut_guides(cropped_part, dpi_by_w)
cropped_part.save(cropped_filepath, 'JPEG', quality=100, subsampling=0)
return cropped_filepath
except OSError as e:
@ -163,6 +130,8 @@ class SheetLayout:
self.blank_space_w = (self.w - self.total_w_content) // 2
self.blank_space_h = (self.h - self.total_h_content) // 2
print(self.__dict__)
def in_to_px(self, inches: float) -> float:
return round(inches * self.dpi)
@ -232,7 +201,28 @@ def create_sheet(cropped_filepaths: List[Path], sheet_filepath: Path, sheet_layo
resample = Image.Resampling.LANCZOS)
sheet.paste(card_im, sheet_layout.topleft_of_card_with_bleed(y_index, x_index))
# TODO: Draw internal guidelines
# Draw internal guidelines
color = 'magenta'
guide_len = sheet_layout.in_to_px(LENGTH_CUT_GUIDE_INCHES)
# -1 because the pixels are in [0, W - 1] and [0, H - 1]
left_x = sheet_layout.left_of_column(x_index, with_bleed=False)
right_x = sheet_layout.right_of_column(x_index, with_bleed=False) - 1
top_y = sheet_layout.top_of_row(y_index, with_bleed=False)
bottom_y = sheet_layout.bottom_of_row(y_index, with_bleed=False) - 1
# Top left
draw.line([(left_x, top_y), (left_x, top_y + guide_len)], fill = color, width = guide_thickness)
draw.line([(left_x, top_y), (left_x + guide_len, top_y)], fill = color, width = guide_thickness)
# Bottom left
draw.line([(left_x, bottom_y), (left_x, bottom_y - guide_len)], fill = color, width = guide_thickness)
draw.line([(left_x, bottom_y), (left_x + guide_len, bottom_y)], fill = color, width = guide_thickness)
# Top right
draw.line([(right_x, top_y), (right_x, top_y + guide_len)], fill = color, width = guide_thickness)
draw.line([(right_x, top_y), (right_x - guide_len, top_y)], fill = color, width = guide_thickness)
# Bottom right
draw.line([(right_x, bottom_y), (right_x, bottom_y - guide_len)], fill = color, width = guide_thickness)
draw.line([(right_x, bottom_y), (right_x - guide_len, bottom_y)], fill = color, width = guide_thickness)
# Save sheet
sheet.save(sheet_filepath, 'JPEG', dpi=(sheet_layout.dpi, sheet_layout.dpi), quality=100, subsampling=0)