如果原始图像没有Alpha层,则可以使用Alpha层作为遮罩将背景转换为白色。当
rotate创建“背景”,这使得它完全透明。
# original imageimg = Image.open('test.png')# converted to have an alpha layerim2 = img.convert('RGBA')# rotated imagerot = im2.rotate(22.2, expand=1)# a white image same size as rotated imagefff = Image.new('RGBA', rot.size, (255,)*4)# create a composite image using the alpha layer of rot as a maskout = Image.composite(rot, fff, rot)# save your work (converting back to mode='1' or whatever..)out.convert(img.mode).save('test2.bmp')


