Thanks to visit codestin.com
Credit goes to github.com

Skip to content

IndexError while drawing on an image #1987

@obskyr

Description

@obskyr

With Pillow 3.2.0 on Python 3 (and on Python 2, with the fix in #1985), running the very same code as in #1592 with the very same image slightly edited, the following traceback occurs:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\PIL\ImagePalette.py", line 97, in getcolor
    return self.colors[color]
KeyError: (255, 255, 255)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "deborder.py", line 30, in <module>
    deborder(sys.argv[1])
  File "deborder.py", line 20, in deborder
    draw.line((topLeft, topRight),       fill=(255, 255, 255))
  File "C:\Python34\lib\site-packages\PIL\ImageDraw.py", line 177, in line
    ink, fill = self._getink(fill)
  File "C:\Python34\lib\site-packages\PIL\ImageDraw.py", line 124, in _getink
    ink = self.palette.getcolor(ink)
  File "C:\Python34\lib\site-packages\PIL\ImagePalette.py", line 108, in getcolor
    self.palette[index+256] = color[1]
IndexError: list assignment index out of range

Before the #1985 fix, the error is an IndexError: list assignment index out of range. After, it persists as an IndexError: bytearray index out of range, however.

From what I can tell, self.palette is shorter than 256 elements, and can therefore not take an assignment to index index+256. In this case, index is 0, as len(self.colors) in getcolor for this image is 0, which I find a bit odd.

Since the image is almost exactly the one used in #1592, this may be related. The difference is this one is edited in ImageMagick to make the outer 1px border white (#FFFFFF).


The very same code as in #1592 causes the error:

# -*- coding: utf-8 -*-

from PIL import Image, ImageDraw

def deborder(filename):
    frame = Image.open(filename)
    frameNo = 0
    while True:
        try:
            frame.seek(frameNo)
        except EOFError:
            break
        draw = ImageDraw.Draw(frame)

        topLeft = (0, 0)
        topRight = (frame.size[0] - 1, 0)
        bottomLeft = (0, frame.size[1] - 1)
        bottomRight = (frame.size[0] - 1, frame.size[1] - 1)

        draw.line((topLeft, topRight),       fill=(255, 255, 255))
        draw.line((topLeft, bottomLeft),     fill=(255, 255, 255))
        draw.line((bottomLeft, bottomRight), fill=(255, 255, 255))
        draw.line((topRight, bottomRight),   fill=(255, 255, 255))

        frameNo += 1

    frame.save('resave-'+filename, save_all=True)

deborder('no.gif')

With the following image, for example (while the error occurs with the exact image in #1592 edited a bit, I chose one with a different glyph to avoid future confusion):

no

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugAny unexpected behavior, until confirmed feature.GIFPalette

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions