簡體   English   中英

有人可以幫助我實現PIL功能嗎?

[英]Can someone help me with my PIL function?

def pad_image(f, width=500, height=None):
    if height==None:
        height = width
    image = Image.new("RGB", (800, 600),  (0, 0, 0, 0))
    image.paste(StringIO(f), (0,0, 50, 50))
    res = StringIO()
    image.save(res, 'JPEG')
    res.seek(0)
    return res

我正在嘗試將我的圖片f粘貼在500x500的白色畫布中。 (在中間)。

到目前為止,這是我的職責,但是我遇到了很多麻煩。 我遇到了很多問題,甚至還沒有碰過高度/寬度部分。

Traceback (most recent call last):
  File "resizer.py", line 23, in <module>
    thumbnail = tools.create_thumbnail(pic,300)
  File "../lib/tools.py", line 84, in create_thumbnail
    thumbnail_file = pad_image(thumbnail_file.read())
  File "../lib/tools.py", line 92, in pad_image
    image.paste(f, (0,0, 50, 50))
  File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 1085, in paste
    im = ImageColor.getcolor(im, self.mode)
  File "/usr/lib/python2.6/dist-packages/PIL/ImageColor.py", line 101, in getcolor
    color = getrgb(color)
  File "/usr/lib/python2.6/dist-packages/PIL/ImageColor.py", line 97, in getrgb
    raise ValueError("unknown color specifier: %r" % color)
ValueError: unknown color specifier: '\xff\xd8\xff\

要粘貼的第一個參數應該是Image而不是StringIO因此

使用image.paste(Image.open(StringIO(f)), (0,0, 50, 50))代替

但您可能應該在粘貼前檢查f的大小,如果它大於50x50,它將僅粘貼左上角

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM