Skip to content

Commit

Permalink
Better crop
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Apr 12, 2019
1 parent 617dded commit 4c40289
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions process
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,11 @@ def crop_image( # pylint: disable=too-many-arguments
image: np.ndarray, x: int, y: int, width: int, height: int,
background: Union[Tuple[int], Tuple[int, int, int]]
) -> np.ndarray:
if x < 0:
indices = np.zeros(-x, dtype=int)
image = np.insert(image, indices, background, axis=1)
x = 0
if y < 0:
indices = np.zeros(-y, dtype=int)
image = np.insert(image, indices, background, axis=0)
y = 0

overflow_x = x + width - image.shape[1]
if overflow_x > 0:
indices = np.zeros(overflow_x, dtype=int) + image.shape[1]
image = np.insert(image, indices, background, axis=1)
overflow_y = y + height - image.shape[0]
if overflow_y > 0:
indices = np.zeros(overflow_y, dtype=int) + int(image.shape[0])
image = np.insert(image, indices, background, axis=0)

return image[y:y + height, x:x + width]
matrice = np.array([
[1, 0, -x],
[0, 1, -y],
])
return cv2.warpAffine(image, matrice, (int(round(height)), int(round(width))), borderValue=background)


class Context: # pylint: disable=too-many-instance-attributes
Expand Down

0 comments on commit 4c40289

Please sign in to comment.