部署时出现SystemError异常

时间:2016-02-12 11:32:17

标签: python django python-2.7

现在,以下代码行在本地服务器上运行正常,但在部署时,它会引发SystemError exception。我一直试图弄清楚异常的来源,但一切看起来都很好,或者有些事情做得不对。如果有人能伸出援助之手,我会很高兴。

这是我的代码

def watermark(img, mark, position=(0, 0), opacity=1, scale=1.0, tile=False, greyscale=False, rotation=0, return_name=False, **kwargs):
    """
    Adds a watermark to an image.
    """
    if opacity < 1:
        mark = reduce_opacity(mark, opacity)

    if type(scale) != tuple:
        scale = determine_scale(scale, img, mark)

    mark = mark.resize(scale, Image.ANTIALIAS)

    if greyscale and mark.mode != 'LA':
        mark = mark.convert('LA')

    rotation = determine_rotation(rotation, mark)
    if rotation != 0:
        # give some leeway for rotation overlapping
        new_w = mark.size[0] * 1.5
        new_h = mark.size[1] * 1.5

        new_mark = Image.new('RGBA', (new_w, new_h), (0,0,0,0))

        # center the watermark in the newly resized image
        new_l = (new_w - mark.size[0]) / 2
        new_t = (new_h - mark.size[1]) / 2
        new_mark.paste(mark, (new_l, new_t))

        mark = new_mark.rotate(rotation)

    position = determine_position(position, img, mark)
    print position

    if img.mode != 'RGBA':
        img = img.convert('RGBA')

    # make sure we have a tuple for a position now
    assert isinstance(position, tuple), 'Invalid position "%s"!' % position

    # create a transparent layer the size of the image and draw the
    # watermark in that layer.
    layer = Image.new('RGBA', img.size, (0,0,0,0))
    if tile:
        first_y = position[1] % mark.size[1] - mark.size[1]
        first_x = position[0] % mark.size[0] - mark.size[0]

        for y in range(first_y, img.size[1], mark.size[1]):
            for x in range(first_x, img.size[0], mark.size[0]):
                layer.paste(mark, (x, y))
    else:
        layer.paste(mark, (0, 0))# Traceback points this line, but it doesn't look wrong to me

    # composite the watermark with the layer
    return Image.composite(layer, img, layer)

堆栈跟踪

 SystemError at /

 new style getargs format but argument is not a tuple

 Request Method:    POST Request URL:   example.com/ Django Version:
    1.7.1 Exception Type:   SystemError Exception Value:    

 new style getargs format but argument is not a tuple

 Exception Location:

    /path/to/app/lib/python2.7/PIL/Image.py in paste, line 1334 

 Python Executable:     /usr/bin/python Python Version:     2.7.5

    /path/to/app/folder/watermark.py in watermark

 214.    layer.paste(mark, (0, 0))

 /path/to/app/lib/python2.7/PIL/Image.py in paste

 1334.    self.im.paste(im, box)

1 个答案:

答案 0 :(得分:0)

我认为问题是由于在开发和部署中使用不同版本的PIL或Pillow引起的。我推荐使用Pillow,因为它得到了更好的支持。

模式“LA”仅为partially supported。这可能是此操作引发异常的原因。

  

PIL还为一些特殊模式提供有限支持,包括LA(L with alpha)

我认为如果你使用RGBA代替LA,代码应该可以工作。 PIL可能无法将LA格式识别为图像,并尝试将其解释为颜色元组。

Image.paste()

  

源代码可以是包含整数或元组的代替图像   像素值。然后该方法用给定的颜色填充该区域。   创建RGB图像时,您还可以使用支持的颜色字符串   通过ImageColor模块。