【问题标题】:Cropping an image with Python Pillow使用 Python Pillow 裁剪图像
【发布时间】:2013-12-20 02:49:32
【问题描述】:

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

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

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

图块不能延伸到图片之外

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 的裁剪示例(我认为应该是相同的)。

【问题讨论】:

标签: python-3.x image-processing python-imaging-library


【解决方案1】:

问题在于逻辑,而不是 Pillow。枕头几乎 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,则 lefttop 都将设置为 25rightbottom 都将设置为 75。因此,您最终会得到一个 50 x 50 的图像,这将是原始图像的确切中心。
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 2020-03-28
  • 2022-06-15
  • 2011-09-02
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多