Rotating image in python: extrapolate background color

有些话、适合烂在心里 提交于 2019-12-24 01:57:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!