2010-12-01 7 views
0
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 흰색 캔버스에 붙여 넣으려고합니다. (중간에).누군가 내 PIL 기능을 도와 줄 수 있습니까?

이것은 지금까지 내 기능이지만 많은 문제가 있습니다. 나는 많은 문제를 겪고 있으며, 높이/너비 부분을 건드리지도 않았다. 붙여

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\ 
+0

'Image.new()'호출에서''(0, 0, 0, 0)은 검은 색 캔버스를 만듭니다. – martineau

답변

0

첫 번째 인수가 아닌 StringIO 그래서

사용 image.paste(Image.open(StringIO(f)), (0,0, 50, 50)) 대신

Image해야하지만 당신은 아마 그것은 단지 왼쪽 상단을 붙여 넣습니다 붙여 넣기 전에 F의 크기를 확인해야합니다 구석이 50x50보다 큰 경우

관련 문제