Python 有人能帮我完成PIL功能吗?

Python 有人能帮我完成PIL功能吗?,python,image,debugging,python-imaging-library,Python,Image,Debugging,Python Imaging Library,我正在尝试将图像粘贴到500x500白色画布中。(在中间) 到目前为止,这是我的功能,但我遇到了很多麻烦。我有很多问题,我甚至没有触及高度/宽度部分 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

我正在尝试将图像粘贴到500x500白色画布中。(在中间)

到目前为止,这是我的功能,但我遇到了很多麻烦。我有很多问题,我甚至没有触及高度/宽度部分

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
回溯(最近一次呼叫最后一次):
文件“resizer.py”,第23行,在
缩略图=工具。创建缩略图(图,300)
文件“./lib/tools.py”,第84行,创建缩略图
缩略图文件=键盘图像(缩略图文件.read())
文件“./lib/tools.py”,第92行,在pad_图像中
图像粘贴(f,(0,0,50,50))
文件“/usr/lib/python2.6/dist packages/PIL/Image.py”,第1085行,粘贴
im=ImageColor.getcolor(im,self.mode)
文件“/usr/lib/python2.6/dist packages/PIL/ImageColor.py”,第101行,在getcolor中
颜色=getrgb(颜色)
文件“/usr/lib/python2.6/dist packages/PIL/ImageColor.py”,第97行,在getrgb中
raise VALUERROR(“未知的颜色说明符:%r”%color)
ValueError:未知的颜色说明符:'\xff\xd8\xff\

要粘贴的第一个参数应该是
图像
而不是
StringIO
so

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

但是,在粘贴f之前,您可能应该检查f的大小。如果f大于50x50,它将只粘贴左上角图像上颜色的尾随
(0,0,0,0)
。new()调用将创建一个黑色画布。
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\