def center_crop(image, crop_size=None):
    """Center crop image.

    Args:
        image: PIL image
        crop_size:  if specified, size of square to center crop
                    otherwise, fit largest square to center of image

    Returns:
        cropped PIL image
    """
    width, height = image.size

    # if crop size not specified, use the largest square in the center of image
    if crop_size is None:
        crop_size = min(height, width)

    # compute crop parameters
    top = int(round((height - crop_size)/2.))
    left = int(round((width - crop_size)/2.))
    bottom = top + crop_size
    right = left + crop_size

    return image.crop((left, top, right, bottom))