parameters: (self, input_file, output_file, output_side_length = 256, fit = True)
docstring: Downsample the image.
docstring_summary: Downsample the image.
docstring_tokens: Downsample the image .
function: def resize_and_crop_image(self, input_file, output_file, output_side_length = 256, fit = True):
          '''Downsample the image.
          '''
          img = Image.open(input_file)
          box = (output_side_length, output_side_length)
          #preresize image with factor 2, 4, 8 and fast algorithm
          factor = 1
          while img.size[0]/factor > 2*box[0] and img.size[1]*2/factor > 2*box[1]:
              factor *=2
          if factor > 1:
              img.thumbnail((img.size[0]/factor, img.size[1]/factor), Image.NEAREST)
  
          #calculate the cropping box and get the cropped part
          if fit:
              x1 = y1 = 0
              x2, y2 = img.size
              wRatio = 1.0 * x2/box[0]
              hRatio = 1.0 * y2/box[1]
              if hRatio > wRatio:
                  y1 = int(y2/2-box[1]*wRatio/2)
                  y2 = int(y2/2+box[1]*wRatio/2)
              else:
                  x1 = int(x2/2-box[0]*hRatio/2)
                  x2 = int(x2/2+box[0]*hRatio/2)
              img = img.crop((x1,y1,x2,y2))
  
          #Resize the image with best quality algorithm ANTI-ALIAS
          img.thumbnail(box, Image.ANTIALIAS)
  
          #save it into a file-like object
          with open(output_file, 'wb') as out:
              img.save(out, 'JPEG', quality=75)
function_tokens: def resize_and_crop_image ( self , input_file , output_file , output_side_length = 256 , fit = True ) : img = Image . open ( input_file ) box = ( output_side_length , output_side_length ) #preresize image with factor 2, 4, 8 and fast algorithm factor = 1 while img . size [ 0 ] / factor > 2 * box [ 0 ] and img . size [ 1 ] * 2 / factor > 2 * box [ 1 ] : factor *= 2 if factor > 1 : img . thumbnail ( ( img . size [ 0 ] / factor , img . size [ 1 ] / factor ) , Image . NEAREST ) #calculate the cropping box and get the cropped part if fit : x1 = y1 = 0 x2 , y2 = img . size wRatio = 1.0 * x2 / box [ 0 ] hRatio = 1.0 * y2 / box [ 1 ] if hRatio > wRatio : y1 = int ( y2 / 2 - box [ 1 ] * wRatio / 2 ) y2 = int ( y2 / 2 + box [ 1 ] * wRatio / 2 ) else : x1 = int ( x2 / 2 - box [ 0 ] * hRatio / 2 ) x2 = int ( x2 / 2 + box [ 0 ] * hRatio / 2 ) img = img . crop ( ( x1 , y1 , x2 , y2 ) ) #Resize the image with best quality algorithm ANTI-ALIAS img . thumbnail ( box , Image . ANTIALIAS ) #save it into a file-like object with open ( output_file , 'wb' ) as out : img . save ( out , 'JPEG' , quality = 75 )