Skip to content

Commit

Permalink
Add crop filter
Browse files Browse the repository at this point in the history
  • Loading branch information
pablerass committed Oct 26, 2023
1 parent b66f7ee commit 230df5a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
18 changes: 17 additions & 1 deletion cartuli/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from .card import CardImage
from .measure import mm, from_str
from .processing import inpaint, straighten
from .processing import inpaint, straighten, crop


class Filter(ABC):
Expand Down Expand Up @@ -75,6 +75,22 @@ def apply(self, card_image: CardImage) -> CardImage:
)


@dataclass(frozen=True)
class CropFilter(Filter):
size: float = 3*mm

def apply(self, card_image: CardImage) -> CardImage:
logger = logging.getLogger('CropFilter')
logger.debug(f'Applying to {card_image}')

return CardImage(
crop(card_image.image, size=card_image.resolution * self.size),
size=card_image.size,
bleed=card_image.bleed,
name=card_image.name
)


def snake_to_class(snake_case_str):
words = snake_case_str.split('_')
camel_case_str = ''.join(word.capitalize() for word in words)
Expand Down
12 changes: 12 additions & 0 deletions cartuli/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,15 @@ def straighten(image: Image.Image, /, outliers_iqr_scale: float = 0.01) -> Image
# TUNE: Maybe new content generated after rotation should be inpainted

return rotated_image


def crop(image: Image.Image, /, size: Size | float | int = 5) -> Image.Image:
logger = logging.getLogger('cartuli.processing')

logger.debug(f"Start {image} image crop", extra={'trace': image})
crop_size = _to_size(size)
crop_box = (crop_size.width, crop_size.height, image.width - crop_size.width, image.height - crop_size.height)
crop_image = image.crop(crop_box)
logger.debug(f"Crop {image}", extra={'trace': crop_image})

return crop_image
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ reportlab==4.*
opencv-python==4.*
pillow==10.*
pyyaml==6.*
nbformat==5.*
carpeta==0.1.0a0
carpeta==0.1.0a0

0 comments on commit 230df5a

Please sign in to comment.