python - 使用 PIL 旋转 RGBA 图像后失去颜色
问题描述
我正在尝试使用 PIL 旋转 RGBA 图像,这就是图像的样子:
但是在旋转之后,它在 alpha = 0 处丢失了所有 RGB 值。
我已经为 rotate() 函数尝试了所有其他重采样类型,但它们甚至使事情变得更糟。
这是原始图像:http ://djosix.com/cell.png
解决方案
简短的回答:
alpha = image.split()[-1]
image = image.convert('RGB').rotate(angle)
image.putalpha(alpha.rotate(angle))
参考源代码:
https://github.com/python-pillow/Pillow/blob/master/src/PIL/Image.py#L2324
在transform()
中,由 调用rotate()
:
if self.mode == "RGBA":
return (
self.convert("RGBa")
.transform(size, method, data, resample, fill, fillcolor)
.convert("RGBA")
)
转换为 RGBa 将根据这部分将 RGB 与 Alpha 相乘:
https://github.com/python-pillow/Pillow/blob/master/src/libImaging/Convert.c#L489
static void
rgbA2rgba(UINT8* out, const UINT8* in, int xsize)
{
int x;
unsigned int alpha, tmp;
for (x = 0; x < xsize; x++) {
alpha = in[3];
*out++ = MULDIV255(*in++, alpha, tmp);
*out++ = MULDIV255(*in++, alpha, tmp);
*out++ = MULDIV255(*in++, alpha, tmp);
*out++ = *in++;
}
}
推荐阅读
- maven - 当我运行 springboot 应用程序时,它总是从错误的路径加载属性
- swift - 将元素添加到 Firestore 数组时获取“类型‘FieldValue’没有成员‘arrayUnion’”
- javafx - JavaFX TableView 列排序有三种状态,为什么?
- c - 如何用c中的复数参数进行计算?
- java - 使用 positionList 压缩连续数字
- android - 如何使用切换摄像头在分区中录制视频并在android中录制后合并
- javascript - 活动类重置回原始状态
- python - 使用 Python 的选择性文本
- node.js - 系统重启时自动重启 Nodejs Socket.io 应用程序
- javascript - 如何使用 jQuery 制作日历(幻灯片天数作为轮播)?没有网格天。只有轮播日