def make_thumbnail(in_fname, out_fname, width, height):
 img = Image.open(in_fname)
 (width_in, height_in) = img.size
 scale_w = (width / float(width_in))
 scale_h = (height / float(height_in))
 if ((height_in * scale_w) <= height):
  scale = scale_w
 else:
  scale = scale_h
 width_sc = int(round((scale * width_in)))
 height_sc = int(round((scale * height_in)))
 img.thumbnail((width_sc, height_sc), Image.ANTIALIAS)
 thumb = Image.new('RGB', (width, height), (255, 255, 255))
 pos_insert = (((width - width_sc) / 2), ((height - height_sc) / 2))
 thumb.paste(img, pos_insert)
 thumb.save(out_fname)

def make_thumbnail ( in_fname , out_fname , width , height ) : img = Image . open ( in_fname ) ( width_in , height_in ) = img . size scale_w = ( width / float ( width_in ) ) scale_h = ( height / float ( height_in ) ) if ( ( height_in * scale_w ) <= height ) : scale = scale_w else : scale = scale_h width_sc = int ( round ( ( scale * width_in ) ) ) height_sc = int ( round ( ( scale * height_in ) ) ) img . thumbnail ( ( width_sc , height_sc ) , Image . ANTIALIAS ) thumb = Image . new ( 'RGB' , ( width , height ) , ( 255 , 255 , 255 ) ) pos_insert = ( ( ( width - width_sc ) / 2 ) , ( ( height - height_sc ) / 2 ) ) thumb . paste ( img , pos_insert ) thumb . save ( out_fname )
make a thumbnail with the same aspect ratio centered in an image with a given width and height .