Coretex
image.py
1 # Copyright (C) 2023 Coretex LLC
2 
3 # This file is part of Coretex.ai
4 
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14 
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 
18 from typing import Tuple
19 
20 from PIL import Image
21 
22 import numpy as np
23 
24 
25 def resizeWithPadding(image: np.ndarray, width: int, height: int) -> Tuple[np.ndarray, int, int]:
26  """
27  Resizes the image while maintaining original aspect ratio,
28  and filling the remaining space with symmetrical padding so
29  that the original image is in the center
30 
31  Parameters
32  ----------
33  image : np.ndarray
34  Input image as an array
35  width : int
36  Width of the output image
37  height : int
38  Height of the output image
39 
40  Returns
41  -------
42  Tuple[np.ndarray, int, int] -> Output image as numpy array,
43  number of pixels of padding from top/bottom and number of pixels
44  of padding from left/right
45  """
46 
47  originalWidth = image.shape[1]
48  originalHeight = image.shape[0]
49 
50  ratio = max(width, height) / max(originalWidth, originalHeight)
51 
52  newWidth = int(originalWidth * ratio)
53  newHeight = int(originalHeight * ratio)
54 
55  resizedImage = Image.fromarray(image).resize((newWidth, newHeight))
56 
57  deltaW = width - newWidth
58  deltaH = height - newHeight
59 
60  top = deltaH // 2
61  left = deltaW // 2
62 
63  paddedImage = Image.new(resizedImage.mode, (width, height), 0)
64  paddedImage.paste(resizedImage, (left, top))
65 
66  return np.array(paddedImage), top, left
67 
68 
69 def cropToWidth(image: np.ndarray) -> np.ndarray:
70  """
71  Crops the image to width while maintaining the center
72 
73  Parameters
74  ----------
75  image : np.ndarray
76  Input image as an array
77 
78  Returns
79  -------
80  np.ndarray -> Output image that has been croped vertically
81  to width
82  """
83 
84  height, width = image.shape[:2]
85  if height > width:
86  startY = (height - width) // 2
87  endY = startY + width
88  image = image[startY:endY, 0:width]
89 
90  return image