问题描述
我安装了Python ,并尝试修剪图像。
I installed Python Pillow and am trying to crop an image.
其他效果非常好(例如,缩略图,模糊图像等)
Other effects work great (for example, thumbnail, blurring image, etc.)
每当我运行下面的代码,我得到错误:
Whenever I run the code below I get the error:
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()
我使用了一个裁剪示例对于,因为我找不到枕头(我认为是相同的) 。
I used a cropping example I found for PIL, because I couldn't find one for Pillow (which I assumed would be the same).
推荐答案
问题在于逻辑,而不是枕头。枕头几乎100%PIL兼容。您创建了一个图像 0 * 0
( left = right& top = bottom
)大小。没有显示可以显示。我的代码如下
The problem is with logic, not Pillow. Pillow is nearly 100% PIL compatible. You created an image of 0 * 0
(left = right & top = bottom
) size. No display can show that. My code is as follows
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()
很可能这不是你想要的,但是应该提供一个明确的想法应该做什么。
Most probably this is not what you want, but this should provide you a clear idea of what should be done.
这篇关于用Python枕头裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!