python裁剪图片尺寸

在 Python 中裁剪图像尺寸可以使用 Python 图像处理库 PIL(Python Imaging Library)或者其他类似的库,如 OpenCV。

下面是使用 PIL 库裁剪图像的代码示例:

from PIL import Image
# 打开图像文件
im = Image.open("example.jpg")
# 计算裁剪区域
width, height = im.size
left = (width - new_width)/2
top = (height - new_height)/2
right = (width + new_width)/2
bottom = (height + new_height)/2
# 裁剪图像
im = im.crop((left, top, right, bottom))
# 保存裁剪后的图像
im.save("example_cropped.jpg")
  •