def set_geometry(im, width_height):
width, height = width_height<EOL>if not width and not height:<EOL><INDENT>return im<EOL><DEDENT>im_width, im_height = im.size<EOL>if (width is None) or (im_width == width):<EOL><INDENT>if (height is None) or (im_height == height):<EOL><INDENT>return im<EOL><DEDENT><DEDENT>ratio = float(im_width) / im_height<EOL>if width and height:<EOL><INDENT>new_width = width<EOL>new_height = int(ceil(width / ratio))<EOL>if new_height < height:<EOL><INDENT>new_height = height<EOL>new_width = int(ceil(height * ratio))<EOL><DEDENT><DEDENT>elif height:<EOL><INDENT>new_width = int(ceil(height * ratio))<EOL>new_height = height<EOL><DEDENT>else:<EOL><INDENT>new_width = width<EOL>new_height = int(ceil(width / ratio))<EOL><DEDENT>im.resize(new_width, new_height)<EOL>box = get_box(new_width, new_height, width, height)<EOL>im.crop(*box, reset_coords=True)<EOL>return im<EOL>
Rescale the image to the new geometry.