Error in draw.rectangle([x1, y1, x2, y2], fill=“Black”) when drawing rectangle with PIL for high dimension images

别等时光非礼了梦想. 提交于 2021-01-28 19:31:47

问题


I'm getting Error in draw.rectangle([x1, y1, x2, y2], fill="Black") when drawing rectangle with PIL python library for high dimension png file(770x1024). But it works for medium size images.

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")
.
.

Sample x1, y1, x2, y2 which throws error : x1: 504.6949750185013 y1: 243.70870971679688 x2: 557.9484252631664 y2: 255.90338134765625 How can I handle this? Do I need to resize programmatically or any other alternative solutions?

Here is the 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)

回答1:


I suspect the problem is that when you open a smallish image, it has fewer pixels and fewer colours and is therefore more likely a palettised image rather than a full-colour RGB image - see here for explanation.

So, I suggest you change to this at the start of your program:

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


来源:https://stackoverflow.com/questions/61359713/error-in-draw-rectanglex1-y1-x2-y2-fill-black-when-drawing-rectangle-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!