def resize_image(image, height, width, channels=None, resize_mode=None):
 if (resize_mode is None):
  resize_mode = 'squash'
 if (resize_mode not in ['crop', 'squash', 'fill', 'half_crop']):
  raise ValueError(('resize_mode "%s" not supported' % resize_mode))
 image = image_to_array(image, channels)
 if ((image.shape[0] == height) and (image.shape[1] == width)):
  return image
 interp = 'bilinear'
 width_ratio = (float(image.shape[1]) / width)
 height_ratio = (float(image.shape[0]) / height)
 if ((resize_mode == 'squash') or (width_ratio == height_ratio)):
  return scipy.misc.imresize(image, (height, width), interp=interp)
 elif (resize_mode == 'crop'):
  if (width_ratio > height_ratio):
   resize_height = height
   resize_width = int(round((image.shape[1] / height_ratio)))
  else:
   resize_width = width
   resize_height = int(round((image.shape[0] / width_ratio)))
  image = scipy.misc.imresize(image, (resize_height, resize_width), interp=interp)
  if (width_ratio > height_ratio):
   start = int(round(((resize_width - width) / 2.0)))
   return image[:, start:(start + width)]
  else:
   start = int(round(((resize_height - height) / 2.0)))
   return image[start:(start + height), :]
 else:
  if (resize_mode == 'fill'):
   if (width_ratio > height_ratio):
    resize_width = width
    resize_height = int(round((image.shape[0] / width_ratio)))
    if (((height - resize_height) % 2) == 1):
     resize_height += 1
   else:
    resize_height = height
    resize_width = int(round((image.shape[1] / height_ratio)))
    if (((width - resize_width) % 2) == 1):
     resize_width += 1
   image = scipy.misc.imresize(image, (resize_height, resize_width), interp=interp)
  elif (resize_mode == 'half_crop'):
   new_ratio = ((width_ratio + height_ratio) / 2.0)
   resize_width = int(round((image.shape[1] / new_ratio)))
   resize_height = int(round((image.shape[0] / new_ratio)))
   if ((width_ratio > height_ratio) and (((height - resize_height) % 2) == 1)):
    resize_height += 1
   elif ((width_ratio < height_ratio) and (((width - resize_width) % 2) == 1)):
    resize_width += 1
   image = scipy.misc.imresize(image, (resize_height, resize_width), interp=interp)
   if (width_ratio > height_ratio):
    start = int(round(((resize_width - width) / 2.0)))
    image = image[:, start:(start + width)]
   else:
    start = int(round(((resize_height - height) / 2.0)))
    image = image[start:(start + height), :]
  else:
   raise Exception(('unrecognized resize_mode "%s"' % resize_mode))
  if (width_ratio > height_ratio):
   padding = ((height - resize_height) / 2)
   noise_size = (padding, width)
   if (channels > 1):
    noise_size += (channels,)
   noise = np.random.randint(0, 255, noise_size).astype('uint8')
   image = np.concatenate((noise, image, noise), axis=0)
  else:
   padding = ((width - resize_width) / 2)
   noise_size = (height, padding)
   if (channels > 1):
    noise_size += (channels,)
   noise = np.random.randint(0, 255, noise_size).astype('uint8')
   image = np.concatenate((noise, image, noise), axis=1)
  return image

def resize_image ( image , height , width , channels = None , resize_mode = None ) : if ( resize_mode is None ) : resize_mode = 'squash' if ( resize_mode not in [ 'crop' , 'squash' , 'fill' , 'half_crop' ] ) : raise ValueError ( ( 'resize_mode "%s" not supported' % resize_mode ) ) image = image_to_array ( image , channels ) if ( ( image . shape [ 0 ] == height ) and ( image . shape [ 1 ] == width ) ) : return image interp = 'bilinear' width_ratio = ( float ( image . shape [ 1 ] ) / width ) height_ratio = ( float ( image . shape [ 0 ] ) / height ) if ( ( resize_mode == 'squash' ) or ( width_ratio == height_ratio ) ) : return scipy . misc . imresize ( image , ( height , width ) , interp = interp ) elif ( resize_mode == 'crop' ) : if ( width_ratio > height_ratio ) : resize_height = height resize_width = int ( round ( ( image . shape [ 1 ] / height_ratio ) ) ) else : resize_width = width resize_height = int ( round ( ( image . shape [ 0 ] / width_ratio ) ) ) image = scipy . misc . imresize ( image , ( resize_height , resize_width ) , interp = interp ) if ( width_ratio > height_ratio ) : start = int ( round ( ( ( resize_width - width ) / 2.0 ) ) ) return image [ : , start : ( start + width ) ] else : start = int ( round ( ( ( resize_height - height ) / 2.0 ) ) ) return image [ start : ( start + height ) , : ] else : if ( resize_mode == 'fill' ) : if ( width_ratio > height_ratio ) : resize_width = width resize_height = int ( round ( ( image . shape [ 0 ] / width_ratio ) ) ) if ( ( ( height - resize_height ) % 2 ) == 1 ) : resize_height += 1 else : resize_height = height resize_width = int ( round ( ( image . shape [ 1 ] / height_ratio ) ) ) if ( ( ( width - resize_width ) % 2 ) == 1 ) : resize_width += 1 image = scipy . misc . imresize ( image , ( resize_height , resize_width ) , interp = interp ) elif ( resize_mode == 'half_crop' ) : new_ratio = ( ( width_ratio + height_ratio ) / 2.0 ) resize_width = int ( round ( ( image . shape [ 1 ] / new_ratio ) ) ) resize_height = int ( round ( ( image . shape [ 0 ] / new_ratio ) ) ) if ( ( width_ratio > height_ratio ) and ( ( ( height - resize_height ) % 2 ) == 1 ) ) : resize_height += 1 elif ( ( width_ratio < height_ratio ) and ( ( ( width - resize_width ) % 2 ) == 1 ) ) : resize_width += 1 image = scipy . misc . imresize ( image , ( resize_height , resize_width ) , interp = interp ) if ( width_ratio > height_ratio ) : start = int ( round ( ( ( resize_width - width ) / 2.0 ) ) ) image = image [ : , start : ( start + width ) ] else : start = int ( round ( ( ( resize_height - height ) / 2.0 ) ) ) image = image [ start : ( start + height ) , : ] else : raise Exception ( ( 'unrecognized resize_mode "%s"' % resize_mode ) ) if ( width_ratio > height_ratio ) : padding = ( ( height - resize_height ) / 2 ) noise_size = ( padding , width ) if ( channels > 1 ) : noise_size += ( channels , ) noise = np . random . randint ( 0 , 255 , noise_size ) . astype ( 'uint8' ) image = np . concatenate ( ( noise , image , noise ) , axis = 0 ) else : padding = ( ( width - resize_width ) / 2 ) noise_size = ( height , padding ) if ( channels > 1 ) : noise_size += ( channels , ) noise = np . random . randint ( 0 , 255 , noise_size ) . astype ( 'uint8' ) image = np . concatenate ( ( noise , image , noise ) , axis = 1 ) return image
resize an image .