You need to enable JavaScript to run this app. 使用Pillow裁剪图片报错SystemError: tile cannot extend outside image如何解决
优惠活动
大模型
产品
解决方案
定价
更多

使用Pillow裁剪图片报错SystemError: tile cannot extend outside image如何解决

问题核心原因

你混淆了Pillow中Image.crop()与ImageOps.crop()的参数定义,这是报错的根本原因:

  • Image.crop(box)的入参box是裁剪后保留区域的绝对坐标,格式为(左, 上, 右, 下),要求右>左、下>上且所有值在图片尺寸范围内。
  • 你调用的ImageOps.crop(image, border)的第二个入参是各边需要裁剪掉的像素宽度,格式为(左裁宽度, 上裁宽度, 右裁宽度, 下裁宽度),不是保留区域的坐标。
报错逻辑推导

你的原图尺寸为1450×644:
你传入的(0, 590, width, 620)参数,意味着要从顶部裁掉590像素、底部裁掉620像素,仅Y轴就需要裁剪1210像素,远大于图片总高度644,裁剪区域超出图片边界,因此抛出SystemError: tile cannot extend outside image错误。

正确实现方案

你需要裁剪保留X轴0到1450、Y轴590到620的区域,两种写法可选:

  1. 用当前ImageOps.crop实现:
    计算得顶部需要裁590像素,底部需要裁644-620=24像素,左右不需要裁剪,代码为:
cropped_image = ImageOps.crop(image, (0, 590, 0, 24))
  1. 更符合你坐标逻辑的原生Image.crop实现(更推荐,不易出错):
    直接传入保留区域的绝对坐标即可:
cropped_image = image.crop((0, 590, 1450, 620))

内容的提问来源于stack exchange,提问作者Mohamed Thabet Ben Aicha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 01:54:07