简体   繁体   中英

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. (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

use image.paste(Image.open(StringIO(f)), (0,0, 50, 50)) instead

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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