Only external bleed

This commit is contained in:
henriquenakashima 2023-07-13 01:06:17 -04:00
parent 89e20fd61e
commit 088181085e
3 changed files with 165 additions and 73 deletions

View file

@ -3,7 +3,7 @@ from pathlib import Path
from PIL import Image
from base_proxy import LEAVE_BLEED_INCHES
from sheet_layout import SheetLayout
from sheet_layout import SheetLayout, CardImageTransfer
W_INCHES_EXPECTED = 2.72
@ -14,28 +14,44 @@ BLEED_INCHES_EXPECTED = 0.12
class InputCardImage:
def __init__(self, path: Path):
self.path = path
self.input_dpi = None
self.original_im = None
def crop(self) -> Image:
def load(self) -> None:
with Image.open(self.path) as im:
# print(im.info)
rgb_im = im.convert('RGB')
w, h = rgb_im.width, rgb_im.height
dpi_by_w = w / W_INCHES_EXPECTED
dpi_by_h = h / H_INCHES_EXPECTED
print(f'By width dpi is {dpi_by_w} dpi')
print(f'Image is {w} x {h}, {dpi_by_w} dpi (by width)')
if dpi_by_h != dpi_by_w:
print(f'dpi by height is different: {dpi_by_h} dpi')
crop_pixels = BLEED_INCHES_EXPECTED * dpi_by_w - LEAVE_BLEED_INCHES * dpi_by_w
crop_pixels_int = round(crop_pixels)
print(f'Image is {w} x {h}, will trim {crop_pixels} pixels (rounded to {crop_pixels_int})')
print(f'By height dpi is different: {dpi_by_h} dpi')
self.input_dpi = dpi_by_w
self.original_im = im
# Crop
crop_box = (crop_pixels_int, crop_pixels_int, w - crop_pixels_int, h - crop_pixels_int)
cropped_part = rgb_im.crop(crop_box)
def get_cropped_and_resized(self, layout: SheetLayout, transfer: CardImageTransfer) -> Image:
new_dpi = layout.dpi
return cropped_part
self.load()
im = self.original_im
# Resize to match sheet DPI
new_w = layout.in_to_px(W_INCHES_EXPECTED)
new_h = layout.in_to_px(H_INCHES_EXPECTED)
im = im.resize((new_w, new_h), resample=Image.Resampling.LANCZOS)
# Crop
top_bleed, bottom_bleed, left_bleed, right_bleed = transfer.bleeds
crop_pixels_leaving_bleed = layout.in_to_px(BLEED_INCHES_EXPECTED - LEAVE_BLEED_INCHES)
crop_pixels_no_bleed = layout.in_to_px(BLEED_INCHES_EXPECTED)
top_crop = crop_pixels_leaving_bleed if top_bleed else crop_pixels_no_bleed
bottom_crop = crop_pixels_leaving_bleed if bottom_bleed else crop_pixels_no_bleed
left_crop = crop_pixels_leaving_bleed if left_bleed else crop_pixels_no_bleed
right_crop = crop_pixels_leaving_bleed if right_bleed else crop_pixels_no_bleed
# Crop
crop_box = (left_crop, top_crop, new_w - right_crop, new_h - bottom_crop)
im = im.crop(crop_box)
def get_cropped_and_resized(self, sheet_layout: SheetLayout) -> Image:
im = self.crop()
im = im.resize((sheet_layout.cropped_image_dest_w, sheet_layout.cropped_image_dest_h), resample=Image.Resampling.LANCZOS)
return im