def centerCropImage(img, targetSize):
    """ Returns a center-cropped image. The returned image is a square image
    with a width and height of `targetSize`. """

    # Resize image while keeping its aspect ratio
    width, height = img.size
    if height < targetSize:
        print(str(height) + " height < targetSize")
    aspectRatio = width / height
    resizedWidth = int(targetSize * aspectRatio)
    resizedWidth = resizedWidth + resizedWidth % 2
    img = img.resize((resizedWidth, targetSize))

    # Apply a center crop by cutting away the same number of pixels at both
    # sides, left and right, of the image
    width, height = img.size
    offsetX = round((width - targetSize) * 0.5)
    return img.crop((offsetX, 0, width - offsetX, height))