问题
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.
回答1:
This could help: https://pillow.readthedocs.io/en/5.2.x/releasenotes/5.2.0.html#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')
Could help if image was rotated by x degrees other than 90,180,270,
来源:https://stackoverflow.com/questions/49510225/rotating-image-in-python-extrapolate-background-color