简体   繁体   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

I am trying to paste my image, f , in a 500x500 white canvas. 我正在尝试将我的图片f粘贴在500x500的白色画布中。 (in the middle). (在中间)。

This is my function so far, but I'm having lots of trouble. 到目前为止,这是我的职责,但是我遇到了很多麻烦。 I'm having so many problems, and I haven't even touched the height/width part. 我遇到了很多问题,甚至还没有碰过高度/宽度部分。

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\

the first argument to paste should be a Image not a StringIO so 要粘贴的第一个参数应该是Image而不是StringIO因此

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

but you should probably check the size of f before pasting it it will only paste the upper left corner if it's bigger than 50x50 但您可能应该在粘贴前检查f的大小,如果它大于50x50,它将仅粘贴左上角

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM