Skip to content Skip to sidebar Skip to footer

Rotating Image In Python: Extrapolate Background Color

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

Solution 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,

Solution 2:

It's not great but you can always fill borders with the average color of the original border

 image.rotate(5, fillcolor=tuple(np.mean(np.array(image)[0,:], axis=0).astype(int)))

Post a Comment for "Rotating Image In Python: Extrapolate Background Color"