2016-11-17 15 views

答えて

1

@Danielあなたは、.thumbnail()を使用してサムネイル画像を作成し、元の画像と同じサイズで、新しいイメージを作成し、新しいイメージにサムネイルを貼り付けることができ、言ったように:

def scale_image(img, factor, bgcolor): 
    # create new image with same mode and size as the original image 
    out = PIL.Image.new(img.mode, img.size, bgcolor) 
    # determine the thumbnail size 
    tw = int(img.width * factor) 
    th = int(img.height * factor) 
    # determine the position 
    x = (img.width - tw) // 2 
    y = (img.height - th) // 2 
    # create the thumbnail image and paste into new image 
    img.thumbnail((tw,th)) 
    out.paste(img, (x,y)) 
    return out 

factorは0と1の間で、bgcolorは新しい画像の背景色です。

例:

img = PIL.Image.open('image.jpg') 
new_img = scale_image(img, 0.5, 'white') 
new_img.show() 
関連する問題