經過PIL修改圖片尺寸

要求

  1. 圖片的修改成正方形
  2. 圖片不要變形
  3. 超出部分能夠白色填充

代碼

from PIL import Image

def format(filePath):
    print "format...."+filePath
    oldImg = Image.open(filePath)
    x, y = oldImg.size
    maxSize = max(x, y)

    newIm = Image.new(oldImg.mode, (maxSize,maxSize), "WHITE")
    newIm.paste(oldImg, ((maxSize-x)/2, (maxSize-y)/2))
    newIm.save(filePath)


if __name__ == '__main__':
    pass
相關文章
相關標籤/搜索