57 lines
2 KiB
Python
57 lines
2 KiB
Python
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
from base_proxy import LEAVE_BLEED_INCHES
|
|
from sheet_layout import SheetLayout, CardImageTransfer
|
|
|
|
|
|
W_INCHES_EXPECTED = 2.72
|
|
H_INCHES_EXPECTED = 3.7
|
|
BLEED_INCHES_EXPECTED = 0.12
|
|
|
|
|
|
class InputCardImage:
|
|
def __init__(self, path: Path):
|
|
self.path = path
|
|
self.input_dpi = None
|
|
self.original_im = None
|
|
|
|
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'Image is {w} x {h}, {dpi_by_w} dpi (by width)')
|
|
if dpi_by_h != dpi_by_w:
|
|
print(f'By height dpi is different: {dpi_by_h} dpi')
|
|
self.input_dpi = dpi_by_w
|
|
self.original_im = im
|
|
|
|
def get_cropped_and_resized(self, layout: SheetLayout, transfer: CardImageTransfer) -> Image:
|
|
new_dpi = layout.dpi
|
|
|
|
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)
|
|
|
|
return im
|