【问题标题】:SystemError exception on deployment部署时出现 SystemError 异常
【发布时间】:2016-02-12 11:32:16
【问题描述】:

现在以下代码行在本地服务器上可以正常工作,但在部署时,它会引发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)

【问题讨论】:

  • 也许您在开发中安装了 Pillow,但在生产中没有安装?
  • 可能是marknew_mark 的模式不同造成的。在粘贴之前,您可以尝试确保两者都是 RGBA。异常信息真的没用,所以我在这里猜测。
  • 谢谢,我试过了,但错误仍然存​​在

标签: python django python-2.7


【解决方案1】:

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

“LA”模式只有partially supported。所以这可能是这个操作引发异常的原因。

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

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

Image.paste()

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

【讨论】:

  • 以上似乎都不是问题,因为我已经尝试过了,但错误仍然存​​在
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-28
  • 2018-02-13
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多