图像填充
from PIL import Image class padBaseImage(): def __init__(self, fimage, pv=(255, 255, 255)): # fimage 图片文件 self.img = Image.open(fimage) self.w = self.img.width self.h = self.img.height self.pv = pv # 填充值 def padding(self, pw, ph): img_ = Image.new('RGB', (self.w + pw, self.h + ph), self.pv) Image.Image.paste(img_, self.img, (pw // 2, ph // 2)) return img_ class padSquareImage(padBaseImage): def __init__(self, fname, pv=(255, 255, 255)): super(padSquareImage, self).__init__(fname, pv) def squre_padding(self): pw = max(0, self.h - self.w) ph = max(0, self.w - self.h) img = self.padding(pw, ph) return img