使用 PIL 在 python 中旋转时指定图像填充颜色并将 Expand 参数设置为 true

发布于 2024-10-21 06:24:24 字数 227 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时光的容颜 2024-10-28 06:24:24

如果您的原始图像没有 Alpha 图层,您可以使用 Alpha 图层作为蒙版将背景转换为白色。当rotate创建“背景”时,它使其完全透明。

# original image
img = Image.open('test.png')
# converted to have an alpha layer
im2 = img.convert('RGBA')
# rotated image
rot = im2.rotate(22.2, expand=1)
# a white image same size as rotated image
fff = Image.new('RGBA', rot.size, (255,)*4)
# create a composite image using the alpha layer of rot as a mask
out = Image.composite(rot, fff, rot)
# save your work (converting back to mode='1' or whatever..)
out.convert(img.mode).save('test2.bmp')

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.

# original image
img = Image.open('test.png')
# converted to have an alpha layer
im2 = img.convert('RGBA')
# rotated image
rot = im2.rotate(22.2, expand=1)
# a white image same size as rotated image
fff = Image.new('RGBA', rot.size, (255,)*4)
# create a composite image using the alpha layer of rot as a mask
out = Image.composite(rot, fff, rot)
# save your work (converting back to mode='1' or whatever..)
out.convert(img.mode).save('test2.bmp')
蝶舞 2024-10-28 06:24:24

rotate 方法中有一个参数 fillcolor 来指定用于扩展区域的颜色:

white = (255,255,255)
pil_image.rotate(angle, PIL.Image.NEAREST, expand = 1, fillcolor = white)

https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.rotate

There is a parameter fillcolor in a rotate method to specify color which will be use for expanded area:

white = (255,255,255)
pil_image.rotate(angle, PIL.Image.NEAREST, expand = 1, fillcolor = white)

https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.rotate

无可置疑 2024-10-28 06:24:24

这是一个受答案启发的工作版本,但它无需打开或保存图像即可工作,并显示如何旋转文本。

这两个图像具有彩色背景和不同于零的 Alpha 通道,以显示正在发生的情况。将两个 Alpha 通道从 92 更改为 0 将使它们完全透明。

from PIL import Image, ImageFont, ImageDraw

text = 'TEST'
font = ImageFont.truetype(r'C:\Windows\Fonts\Arial.ttf', 50)
width, height = font.getsize(text)

image1 = Image.new('RGBA', (200, 150), (0, 128, 0, 92))
draw1 = ImageDraw.Draw(image1)
draw1.text((0, 0), text=text, font=font, fill=(255, 128, 0))

image2 = Image.new('RGBA', (width, height), (0, 0, 128, 92))
draw2 = ImageDraw.Draw(image2)
draw2.text((0, 0), text=text, font=font, fill=(0, 255, 128))

image2 = image2.rotate(30, expand=1)

px, py = 10, 10
sx, sy = image2.size
image1.paste(image2, (px, py, px + sx, py + sy), image2)

image1.show()

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.

from PIL import Image, ImageFont, ImageDraw

text = 'TEST'
font = ImageFont.truetype(r'C:\Windows\Fonts\Arial.ttf', 50)
width, height = font.getsize(text)

image1 = Image.new('RGBA', (200, 150), (0, 128, 0, 92))
draw1 = ImageDraw.Draw(image1)
draw1.text((0, 0), text=text, font=font, fill=(255, 128, 0))

image2 = Image.new('RGBA', (width, height), (0, 0, 128, 92))
draw2 = ImageDraw.Draw(image2)
draw2.text((0, 0), text=text, font=font, fill=(0, 255, 128))

image2 = image2.rotate(30, expand=1)

px, py = 10, 10
sx, sy = image2.size
image1.paste(image2, (px, py, px + sx, py + sy), image2)

image1.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文