在python中旋转图像:推断背景颜色 [英] Rotating image in python: extrapolate background color
问题描述
我正在使用以下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')
这将进行以下转换:
到
如您所见,背景为黑色. 这个问题询问如何用特定颜色填充背景.但是,我想用图像的颜色填充背景.因为我需要旋转许多图像,所以我不想将背景设置为固定颜色.
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.
推荐答案
这可能会有所帮助: 5.2.0及更高版本的枕头,一个名为fillcolor
的新命名参数已添加到Image.rotate
.此颜色指定要在旋转的图像之外的区域中使用的背景颜色.
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')
如果图像旋转了90,180,270以外的x度,可能会有所帮助
Could help if image was rotated by x degrees other than 90,180,270,
这篇关于在python中旋转图像:推断背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!