使用Python Pillow裁剪图像

时间:2013-12-03 20:53:25

标签: python-imaging-library pillow

我安装了Python Pillow并尝试裁剪图片。

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

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

  

tile无法扩展到图像外

test_image = test_media.file
original = Image.open(test_image)

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

cropped_example.show()

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

1 个答案:

答案 0 :(得分:49)

问题在于逻辑,而不是枕头。枕头几乎100%兼容PIL。您创建了0 * 0left = 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()

很可能这不是你想要的,但这应该让你清楚地了解应该做些什么。