Skip to content

Commit

Permalink
Zoom instead of crop/pan
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglemansweep committed Jun 9, 2024
1 parent c6620a4 commit 2848451
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions inkyframeweb/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ def glob_images(
return image_files


def load_and_resize_image(image_path: Path, size: tuple[int, int]) -> Image.Image:
def load_and_resize_image(
image_path: Path, size: tuple[int, int], fill: bool = False
) -> Image.Image:
image = Image.open(image_path)
image = resize_image_aspect(image, size)
if fill:
image = resize_image_fill(image, size)
else:
image = resize_image_aspect(image, size)
image = image.convert("RGB")
return image

Expand Down Expand Up @@ -58,6 +63,23 @@ def resize_image_aspect(image: Image.Image, size: tuple[int, int]) -> Image.Imag
return new_image


def resize_image_fill(image: Image.Image, size: tuple[int, int]) -> Image.Image:
original_width, original_height = image.size
target_width, target_height = size
new_image = Image.new("RGB", size, color="black")
if original_width > original_height:
new_height = target_height
new_width = int(original_width * (new_height / original_height))
image = image.resize((new_width, new_height), Image.LANCZOS) # type: ignore[attr-defined]
new_image.paste(image, ((target_width - new_width) // 2, 0))
else:
new_width = target_width
new_height = int(original_height * (new_width / original_width))
image = image.resize((new_width, new_height), Image.LANCZOS) # type: ignore[attr-defined]
new_image.paste(image, (0, (target_height - new_height) // 2))
return new_image


def bytes_to_pil(image_bytes):
return Image.open(io.BytesIO(image_bytes))

Expand Down Expand Up @@ -113,7 +135,7 @@ def render_image(
overlay_color: str = "white",
) -> io.BytesIO:
# Load and resize image
image = load_and_resize_image(image_file, size)
image = load_and_resize_image(image_file, size, fill=True)
# Build overlay
if not overlay_format == "":
now = datetime.now()
Expand Down

0 comments on commit 2848451

Please sign in to comment.