-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg_utils.py
35 lines (29 loc) · 1.06 KB
/
img_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2019 theo <theo@not-arch-linux>
#
# Distributed under terms of the MIT license.
import numpy as np
from keras.preprocessing import image
from PIL import Image
"""
Several image manipulation utility functions
"""
'''
Crops the given image over the given bounding box coordinates, and applies
zero-padding to the rest of the image. The returned image in fact has the
same dimensions as the given image
'''
def crop_and_pad(img, corner_min, corner_max, centered=True):
cropped = np.zeros(img.shape, dtype=img.dtype)
crop = img[corner_min[1]:corner_max[1], corner_min[0]:corner_max[0],:]
if centered:
startW = int((img.shape[1] - crop.shape[1]) / 2)
startH = int((img.shape[0] - crop.shape[0]) / 2)
cropped[startH:startH+crop.shape[0], startW:startW+crop.shape[1],:] = crop
else:
cropped[corner_min[1]:corner_max[1], corner_min[0]:corner_max[0],:] = crop
assert cropped.shape == img.shape, "Cropped image has been resized!"
return cropped