def crop_image(data, type='top', ratio=False, thumbnail_ratio=None, image_format='PNG'):
 if (not data):
  return False
 image_stream = Image.open(StringIO.StringIO(data.decode('base64')))
 output_stream = StringIO.StringIO()
 (w, h) = image_stream.size
 new_h = h
 new_w = w
 if ratio:
  (w_ratio, h_ratio) = ratio
  new_h = ((w * h_ratio) / w_ratio)
  new_w = w
  if (new_h > h):
   new_h = h
   new_w = ((h * w_ratio) / h_ratio)
 if (type == 'top'):
  cropped_image = image_stream.crop((0, 0, new_w, new_h))
  cropped_image.save(output_stream, format=image_format)
 elif (type == 'center'):
  cropped_image = image_stream.crop((((w - new_w) / 2), ((h - new_h) / 2), ((w + new_w) / 2), ((h + new_h) / 2)))
  cropped_image.save(output_stream, format=image_format)
 elif (type == 'bottom'):
  cropped_image = image_stream.crop((0, (h - new_h), new_w, h))
  cropped_image.save(output_stream, format=image_format)
 else:
  raise ValueError('ERROR: invalid value for crop_type')
 if thumbnail_ratio:
  thumb_image = Image.open(StringIO.StringIO(output_stream.getvalue()))
  thumb_image.thumbnail(((new_w / thumbnail_ratio), (new_h / thumbnail_ratio)), Image.ANTIALIAS)
  thumb_image.save(output_stream, image_format)
 return output_stream.getvalue().encode('base64')

def crop_image ( data , type = 'top' , ratio = False , thumbnail_ratio = None , image_format = 'PNG' ) : if ( not data ) : return False image_stream = Image . open ( StringIO . StringIO ( data . decode ( 'base64' ) ) ) output_stream = StringIO . StringIO ( ) ( w , h ) = image_stream . size new_h = h new_w = w if ratio : ( w_ratio , h_ratio ) = ratio new_h = ( ( w * h_ratio ) / w_ratio ) new_w = w if ( new_h > h ) : new_h = h new_w = ( ( h * w_ratio ) / h_ratio ) if ( type == 'top' ) : cropped_image = image_stream . crop ( ( 0 , 0 , new_w , new_h ) ) cropped_image . save ( output_stream , format = image_format ) elif ( type == 'center' ) : cropped_image = image_stream . crop ( ( ( ( w - new_w ) / 2 ) , ( ( h - new_h ) / 2 ) , ( ( w + new_w ) / 2 ) , ( ( h + new_h ) / 2 ) ) ) cropped_image . save ( output_stream , format = image_format ) elif ( type == 'bottom' ) : cropped_image = image_stream . crop ( ( 0 , ( h - new_h ) , new_w , h ) ) cropped_image . save ( output_stream , format = image_format ) else : raise ValueError ( 'ERROR: invalid value for crop_type' ) if thumbnail_ratio : thumb_image = Image . open ( StringIO . StringIO ( output_stream . getvalue ( ) ) ) thumb_image . thumbnail ( ( ( new_w / thumbnail_ratio ) , ( new_h / thumbnail_ratio ) ) , Image . ANTIALIAS ) thumb_image . save ( output_stream , image_format ) return output_stream . getvalue ( ) . encode ( 'base64' )
return the specified section of the image .