【问题标题】:Error in draw.rectangle([x1, y1, x2, y2], fill="Black") when drawing rectangle with PIL for high dimension images使用 PIL 为高维图像绘制矩形时 draw.rectangle([x1, y1, x2, y2], fill="Black") 出错
【发布时间】:2020-08-05 03:42:24
【问题描述】:

使用 PIL python 库为高维 png 文件 (770x1024) 绘制矩形时,draw.rectangle([x1, y1, x2, y2], fill="Black") 出现错误。但它适用于中等大小的图像。

img = Image.open(BytesIO(file_byte_string))
width, height = img.size
.
.
if(doc.pages):
  page = doc.pages[0]
.
.
for field in page.form.fields: 
  if(field.key and field.value):

.
.    
  x1 = field.value.geometry.boundingBox.left*width
  y1 = field.value.geometry.boundingBox.top*height-2
  x2 = x1 + (field.value.geometry.boundingBox.width*width)+5
  y2 = y1 + (field.value.geometry.boundingBox.height*height)+2
  draw = ImageDraw.Draw(img) 
  draw.rectangle([x1, y1, x2, y2], fill="Black")
.
.

样本 x1、y1、x2、y2 会引发错误:x1: 504.6949750185013 y1: 243.70870971679688 x2: 557.9484252631664 y2: 255.90338134765625 我该如何处理?我是否需要以编程方式或任何其他替代解决方案调整大小?

这里是 StackTrace:

======================================================================
ERROR: testDataPull (__main__.TestLambda)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python\Python38\lib\site-packages\PIL\ImagePalette.py", line 99, in getcolor
    return self.colors[color]
KeyError: (0, 0, 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\TestLambda\lambda_test.py", line 22, in testDataPull
    lambda_handler(event, "")
  File "c:\TestLambda\lambda_function.py", line 21, in lambda_handler
    ret_str = redact_go(my_bucket,my_key)
  File "c:\TestLambda\redact.py", line 63, in redact_go
    draw.rectangle([x1, y1, x2, y2], fill="Black")
  File "C:\Python\Python38\lib\site-packages\PIL\ImageDraw.py", line 246, in rectangle
    ink, fill = self._getink(outline, fill)
  File "C:\Python\Python38\lib\site-packages\PIL\ImageDraw.py", line 118, in _getink
    fill = self.palette.getcolor(fill)
  File "C:\Python\Python38\lib\site-packages\PIL\ImagePalette.py", line 109, in getcolor
    self.palette[index + 256] = color[1]
IndexError: bytearray index out of range

----------------------------------------------------------------------
Ran 1 test in 20.892s

FAILED (errors=1)

【问题讨论】:

  • 什么错误?请同时提供堆栈跟踪
  • 在绘图前立即打印x1,y1,x2,y2 的值。
  • 我已经用错误详情 @h4z3 更新了查询
  • x1: 504.6949750185013 y1: 243.70870971679688 x2: 557.9484252631664 y2: 255.90338134765625 @MarkSetchell

标签: python python-3.x python-imaging-library redaction


【解决方案1】:

我怀疑问题在于,当您打开一张较小的图像时,它的像素和颜色更少,因此更有可能是调色板图像而不是全彩 RGB 图像 - 请参阅 here 以获取解释。

所以,我建议你在程序开始时改成这个:

# Open image and ensure it is 3-channel RGB, not palettised
img = Image.open(BytesIO(file_byte_string)).convert('RGB')

【讨论】:

  • 背景 = Image.open(f'/app/public{input}').convert('RGB')
猜你喜欢
  • 2022-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-18
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 1970-01-01
相关资源
最近更新 更多