python怎么填充背景颜色_在python中旋转图像:推断背景颜色

I am rotating an image with the following python code:

from PIL import Image

img = Image.open('banana.jpg')

rotated = img.rotate(10)

rotated.save('banana-rotated.jpg')

This makes the following transformation:

to

As you can see, the background is black. This question asks how to fill the background with a specific color. However, I would like to fill the background with the color of the image. Because I need to rotate many images, I do not want to set the background to a fixed color.

Is there some way to extend the image at the edges, with a color extrapolated from the image? Ideally, this would also work if the image does not have a uniform background.

解决方案

Pillow 5.2.0 onwards, A new named parameter called fillcolor, has been added to Image.rotate. This color specifies the background color to use in the area outside the rotated image.

image.rotate(45, fillcolor='white')

Could help if image was rotated by x degrees other than 90,180,270,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值