Python imaging library 裁剪图像的中心

Python imaging library 裁剪图像的中心,python-imaging-library,python-2.x,Python Imaging Library,Python 2.x,如何从PIL使用函数crop()来裁剪224*224图像的中心 给定输入图像: 320*240,裁剪此尺寸为224*224的图像的中心 预期输出: 尺寸为224*224的图像的裁剪中心,从该图像开始,彩色部分为224x224,黑色背景为320x240 我只需要使用numpy进行如下裁剪: #!/usr/local/bin/python3 from PIL import Image import numpy as np # Open the image and convert to numpy

如何从
PIL
使用函数
crop()
来裁剪224*224图像的中心

给定输入图像:

320*240,裁剪此尺寸为224*224的图像的中心

预期输出:


尺寸为224*224的图像的裁剪中心,从该图像开始,彩色部分为224x224,黑色背景为320x240

我只需要使用
numpy
进行如下裁剪:

#!/usr/local/bin/python3
from PIL import Image
import numpy as np

# Open the image and convert to numpy array
im=Image.open('start.png')
im=np.array(im)

# Work out where top left corner is
y=int((320-224)/2)
x=int((240-224)/2)

# Crop, convert back from numpy to PIL Image and and save
cropped=im[x:x+224,y:y+224]
Image.fromarray(cropped).save('result.png')

我的回答解决了你的问题吗?如果是这样,请考虑接受它作为您的答案-点击空心蜱/支票旁边的选票计数。如果没有,请说出什么不起作用,以便我或其他人可以进一步帮助您。谢谢