2016-02-12 136 views
1

現在,以下代碼行可在本地服務器上正常工作,但在部署時,它會提升SystemError exception。我一直在試圖找出異常來自哪裏,但一切看起來都很好,或者有些事情可能不正確。如果有人能夠伸出援手,會很高興。部署時出現SystemError異常

這裏是我的代碼

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

也許你在開發中安裝了Pillow,但是沒有在生產中安裝? –

+0

這可能是由具有不同模式的'mark'和'new_mark'造成的。在粘貼之前,您可以嘗試確保兩者都是RGBA。這個異常消息確實沒有幫助,所以我在這裏猜測。 –

+0

謝謝,我試過了,但是錯誤仍然存​​在 – phourxx

回答

0

我認爲這個問題是通過開發和部署不同版本的PIL或枕頭造成的。我建議使用Pillow,因爲它更好的支持。

模式「LA」只有partially supported。所以這可能是此操作引發異常的原因。

PIL還提供了一些特殊的方式提供有限的支持,其中包括LA(L阿爾法)

我想,如果你使用RGBA,而不是LA的代碼應工作。 PIL可能不會將LA格式識別爲圖像,並嘗試將其解釋爲顏色元組。

Image.paste()

相反的圖像的,源可以是含有 像素值的整數或元組。該方法然後用給定的顏色填充該區域。 創建RGB圖像時,您還可以使用ImageColor模塊支持的顏色字符串 。

+0

以上都沒有問題,因爲我已經嘗試過了,錯誤仍然存​​在 – phourxx