Python 3.x 用Python裁剪图像

Python 3.x 用Python裁剪图像,python-3.x,image-processing,python-imaging-library,Python 3.x,Image Processing,Python Imaging Library,我安装了Python,正在尝试裁剪图像 其他效果也很好(例如,缩略图、模糊图像等) 每当我运行下面的代码时,都会出现错误: 平铺无法延伸到图像外部 我使用了我找到的一个裁剪示例,因为我找不到枕头的裁剪示例(我假设是相同的)。问题在于逻辑,而不是枕头。枕头几乎100%与PIL兼容。您创建了一个大小为0*0(left=right&top=bottom)的图像。没有显示器能显示这一点。我的代码如下 from PIL import Image test_image = "Fedora_19_with_

我安装了Python,正在尝试裁剪图像

其他效果也很好(例如,缩略图、模糊图像等)

每当我运行下面的代码时,都会出现错误:

平铺无法延伸到图像外部


我使用了我找到的一个裁剪示例,因为我找不到枕头的裁剪示例(我假设是相同的)。

问题在于逻辑,而不是枕头。枕头几乎100%与PIL兼容。您创建了一个大小为
0*0
left=right&top=bottom
)的图像。没有显示器能显示这一点。我的代码如下

from PIL import Image

test_image = "Fedora_19_with_GNOME.jpg"
original = Image.open(test_image)
original.show()

width, height = original.size   # Get dimensions
left = width/4
top = height/4
right = 3 * width/4
bottom = 3 * height/4
cropped_example = original.crop((left, top, right, bottom))

cropped_example.show()

很可能这不是你想要的。但这会引导您清楚地知道应该做什么。

可能的复制对于任何想知道这是什么的人来说,它会剪切图像的外部边缘,留下原始图像的中心。裁剪图像的尺寸最终为原始图像尺寸的一半。下面是一个示例:如果要剪切的图像是100 x 100,
都将设置为
25
都将设置为
75
。因此,您将得到一个50 x 50的图像,它将是原始图像的精确中心。
from PIL import Image

test_image = "Fedora_19_with_GNOME.jpg"
original = Image.open(test_image)
original.show()

width, height = original.size   # Get dimensions
left = width/4
top = height/4
right = 3 * width/4
bottom = 3 * height/4
cropped_example = original.crop((left, top, right, bottom))

cropped_example.show()