Skip to content

Commit

Permalink
plot/save resized image
Browse files Browse the repository at this point in the history
  • Loading branch information
pme0 committed Jun 22, 2023
1 parent 6a41bff commit cca21d9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion deeplightning/viz/image/bboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def plot_image_and_bboxes(
resize: int = None,
save_path: str = None,
show_image: bool = True
):
):
"""Plot an image together with a set of bounding boxes and class labels
Example:
Expand Down
47 changes: 47 additions & 0 deletions deeplightning/viz/image/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Tuple
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import imagesize
from PIL import Image
import numpy as np

from deeplightning.utils.io_local import read_image


def plot_resized_image(
image_fp: str,
resize: Tuple[int,int] = None,
display_image: bool = True,
display_size: Tuple[int,int] = None,
save_fp : str = None,
):
"""Plot resized image & saves with new exact pixel size.
Parameters
----------
image_fp : input image filepath
resize : resize image to (width, height), in pixels; also used
to save the image with that exact pixel size (no borders)
display_image : whether to display image
display_size : size of displayed image, in inches (width, height)
save_fp : output image save filepath
"""
assert isinstance(resize, tuple) and len(resize) == 2

image = Image.open(image_fp)
image = image.convert('RGB')
image = image.resize(resize)
new_image = Image.new("RGB", resize, (255,255,255))
position = ((resize[0] - image.width) // 2, (resize[1] - image.height) // 2)
new_image.paste(image, position)

if save_fp:
new_image.save(save_fp)

if display_image:
fig = plt.figure(figsize=display_size)
plt.imshow(new_image)
plt.axis("off")
plt.show()

plt.close()

0 comments on commit cca21d9

Please sign in to comment.