ADD: basic min axis content resizing

This commit is contained in:
dianavermilya
2015-07-28 17:00:28 -04:00
committed by Madison May
parent 1d42d6defe
commit d00b669a16
5 changed files with 54 additions and 5 deletions
+21 -4
View File
@@ -10,7 +10,7 @@ from indicoio.utils.errors import IndicoError, DataStructureException
B64_PATTERN = re.compile("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)")
def image_preprocess(image, size=(48,48), batch=False):
def image_preprocess(image, size=(48,48), min_axis=None, batch=False):
"""
Takes an image and prepares it for sending to the api including
resizing and image data/structure standardizing.
@@ -41,9 +41,7 @@ def image_preprocess(image, size=(48,48), batch=False):
else:
raise IndicoError("Image must be a filepath, base64 encoded string, or a numpy array")
# image resizing
if size:
out_image = out_image.resize(size)
out_image = resize_image(out_image, size, min_axis)
# convert to base64
temp_output = StringIO.StringIO()
@@ -54,6 +52,25 @@ def image_preprocess(image, size=(48,48), batch=False):
return base64.b64encode(output_s)
def resize_image(image, size, min_axis):
if size:
image = image.resize(size)
if min_axis:
min_idx, other_idx = (0,1) if image.size[0] < image.size[1] else (1,0)
aspect = image.size[other_idx]/float(image.size[min_idx])
if aspect > 10:
warnings.warn(
"An aspect ratio greater than 10:1 is not recommended",
Warning
)
size_arr = [0,0]
size_arr[min_idx] = min_axis
size_arr[other_idx] = int(min_axis * aspect)
image = image.resize(tuple(size_arr))
return image
def get_list_dimensions(_list):
"""
Takes a nested list and returns the size of each dimension followed