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 560734e commit cc41637
Showing 1 changed file with 18 additions and 48 deletions.
66 changes: 18 additions & 48 deletions inkyframeweb/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,61 +29,31 @@ def load_and_resize_image(
image = Image.open(image_path)
if aspect == "zoom":
image = resize_image_zoom(image, size)
else:
image = resize_image_crop(image, size)
image = image.convert("RGB")
return image


def resize_image_zoom(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 resize_image_crop(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_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))
new_width, new_height = size
# Calculate aspect ratios
original_aspect = original_width / original_height
new_aspect = new_width / new_height
# Determine scaling factor and resize
if new_aspect > original_aspect:
scale_factor = new_height / original_height
else:
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))
return new_image


def resize_image_zoom2(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_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))
else:
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))
return new_image
scale_factor = new_width / original_width
new_size = (int(original_width * scale_factor), int(original_height * scale_factor))
image = image.resize(new_size, Image.LANCZOS) # type: ignore[attr-defined]
# Calculate coordinates for cropping
left = int((new_size[0] - new_width) / 2)
top = int((new_size[1] - new_height) / 2)
right = left + new_width
bottom = top + new_height
# Crop the image to the desired size
image = image.crop((left, top, right, bottom))
return image


def bytes_to_pil(image_bytes):
Expand Down

0 comments on commit cc41637

Please sign in to comment.