使用 PIL 在 python 中旋转时指定图像填充颜色并将 Expand 参数设置为 true
我正在尝试使用 PIL 在 Python 中旋转图像并将 Expand 参数设置为 true。看起来,当我的图像背景是黑色时,保存为 bmp 的结果图像会比图像背景为白色时小很多,然后我将由于扩展而替换为白色的黑色。无论哪种情况,我的原始图像始终是两种颜色,现在我需要较小的文件大小,因为我将这些图像放在嵌入式设备上。
如果我可以在扩展时强制旋转以填充另一种颜色,或者是否有其他方法可以旋转我的图片以使其变小,有什么想法吗?
I'm trying to rotate an image in Python using PIL and having the expand argument to true. It seems that when the background of my image is black, the resulting image saved as a bmp will be a lot smaller than if I have a white background for my image, and then I replace the black due to expand with white. In either case, my original image is always of two colors, and right now i need the file size to be small, since I'm putting these images on an embedded device.
Any ideas if i can force rotate to fill in another color when expanding or if there is another way to rotate my picture in order to make it small?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的原始图像没有 Alpha 图层,您可以使用 Alpha 图层作为蒙版将背景转换为白色。当
rotate
创建“背景”时,它使其完全透明。If your original image has no alpha layer, you can use an alpha layer as a mask to convert the background to white. When
rotate
creates the "background", it makes it fully transparent.rotate
方法中有一个参数fillcolor
来指定用于扩展区域的颜色:https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.rotate
There is a parameter
fillcolor
in arotate
method to specify color which will be use for expanded area:https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.rotate
这是一个受答案启发的工作版本,但它无需打开或保存图像即可工作,并显示如何旋转文本。
这两个图像具有彩色背景和不同于零的 Alpha 通道,以显示正在发生的情况。将两个 Alpha 通道从 92 更改为 0 将使它们完全透明。
Here is a working version, inspired by the answer, but it works without opening or saving images and shows how to rotate a text.
The two images have colored background and alpha channel different from zero to show what's going on. Changing the two alpha channels from 92 to 0 will make them completely transparent.