您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用PIL在python中旋转时指定图像填充颜色,并将expand参数设置为true

使用PIL在python中旋转时指定图像填充颜色,并将expand参数设置为true

如果原始图像没有Alpha层,则可以使用Alpha层作为遮罩将背景转换为白色。当rotate创建“背景”,这使得它完全透明。

# original image
img = Image.open('test.png')
# converted to have an alpha layer
im2 = img.convert('RGBA')
# rotated image
rot = im2.rotate(22.2, expand=1)
# a white image same size as rotated image
fff = Image.new('RGBA', rot.size, (255,)*4)
# create a composite image using the alpha layer of rot as a mask
out = Image.composite(rot, fff, rot)
# save your work (converting back to mode='1' or whatever..)
out.convert(img.mode).save('test2.bmp')
python 2022/1/1 18:32:13 有336人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶