我是靠谱客的博主 包容信封,最近开发中收集的这篇文章主要介绍pythonfillcolor_是否有另一种方法可以用白色填充旋转图像外部的区域? 'fillcolor'不适用于旧版本的Python...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

I want to rotate a black and white image. I am trying to use the rotate function as follows:

image.rotate(angle, fillcolor=255)

I am required to older versions of Python and Pillow, and they do not support the 'fillcolor' argument. I cannot upgrade to the newer versions due to certain restrictions and cannot use any external libraries.

Is there another way to fill the area outside the rotated image with white color using Pillow?

Rotated image has black color in the area outside the rotated part. I want to fill it with white color.

解决方案

You can try Interpolating the Original Image, with the cropped one via Image.composite() to get rid of the black bars/borders.

from PIL import Image

img = Image.open(r"Image_Path").convert("RGBA")

angle = 30

img = img.rotate(angle)

new_img = Image.new('RGBA', img.size, 'white')

Alpha_Image = Image.composite(img, new_img, img)

Alpha_Image = Alpha_Image.convert(img.mode)

Alpha_Image.show()

The above code takes in an Image, converts it into mode RGBA (Alpha is required for this process), and then rotates the Image by 30 degrees. After that It creates a empty Image object of mode RGBA of the same dimensions as the original image, with each pixel having a default value of 255 each channel (i.e Pure white for RGB, and Full Opacity in the context of Alpha/Transparency). Then Interpolates the original image with this empty one using the mask of original Image (we are using the transparency mask of the first image). This results in the Desired images, where black bars/edges are replaced by white. In the end we convert the image color space to the original one.

ORIGINAL IMAGE:-

IMAGE AFTER ROTATING 30 DEGREES:-

最后

以上就是包容信封为你收集整理的pythonfillcolor_是否有另一种方法可以用白色填充旋转图像外部的区域? 'fillcolor'不适用于旧版本的Python...的全部内容,希望文章能够帮你解决pythonfillcolor_是否有另一种方法可以用白色填充旋转图像外部的区域? 'fillcolor'不适用于旧版本的Python...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(33)

评论列表共有 0 条评论

立即
投稿
返回
顶部