python中旋转图像:推断背景颜色

2024-05-04

我使用以下 python 代码旋转图像:

from PIL import Image

img = Image.open('banana.jpg')
rotated = img.rotate(10)
rotated.save('banana-rotated.jpg')

这会产生以下转变:

enter image description here to enter image description here

如您所见,背景是黑色的。这个问题 https://stackoverflow.com/questions/5252170/specify-image-filling-color-when-rotating-in-python-with-pil-and-setting-expand询问如何用特定颜色填充背景。但是,我想用图像的颜色填充背景。因为我需要旋转很多图像,所以我不想将背景设置为固定颜色。

有什么办法可以扩展图像的边缘, 从图像中推断出颜色?理想情况下,如果图像没有统一的背景,这也可以工作。


这可以帮助:https://pillow.readthedocs.io/en/5.2.x/releasenotes/5.2.0.html#image-rotate https://pillow.readthedocs.io/en/5.2.x/releasenotes/5.2.0.html#image-rotate

Pillow 5.2.0 开始,一个新的命名参数称为fillcolor,已添加到Image.rotate。此颜色指定在旋转图像外部的区域中使用的背景颜色。

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

如果图像旋转 x 度而不是 90,180,270,可能会有所帮助,

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python中旋转图像:推断背景颜色 的相关文章

随机推荐