2017-08-16 12:24:29 +00:00
|
|
|
import PIL
|
2017-08-17 13:32:38 +00:00
|
|
|
import numpy as np
|
2017-08-16 12:24:29 +00:00
|
|
|
|
2017-08-17 13:32:38 +00:00
|
|
|
def get_square(img, pos):
|
|
|
|
"""Extract a left or a right square from PILimg"""
|
2017-08-16 12:24:29 +00:00
|
|
|
"""shape : (H, W, C))"""
|
2017-08-17 13:32:38 +00:00
|
|
|
img = np.array(img)
|
|
|
|
|
2017-08-16 12:24:29 +00:00
|
|
|
h = img.shape[0]
|
|
|
|
w = img.shape[1]
|
|
|
|
|
2017-08-17 13:32:38 +00:00
|
|
|
if pos == 0:
|
|
|
|
return img[:, :h]
|
|
|
|
else:
|
|
|
|
return img[:, -h:]
|
2017-08-16 12:24:29 +00:00
|
|
|
|
|
|
|
def resize_and_crop(pilimg, scale=0.5, final_height=640):
|
|
|
|
w = pilimg.size[0]
|
|
|
|
h = pilimg.size[1]
|
|
|
|
newW = int(w * scale)
|
|
|
|
newH = int(h * scale)
|
|
|
|
diff = newH - final_height
|
|
|
|
|
|
|
|
img = pilimg.resize((newW, newH))
|
|
|
|
img = img.crop((0, diff // 2, newW, newH - diff // 2))
|
|
|
|
return img
|