使用PIL在python中旋转并将expand参数设置为true时指定图像填充颜色
我正在尝试使用PIL在Python中旋转一个图像,并将expand参数设置为true。看起来,当我的图像背景为黑色时,保存为bmp的结果图像将比我的图像背景为白色时小得多,然后我将黑色替换为白色。在任何一种情况下,我的原始图像总是两种颜色,现在我需要文件的大小要小,因为我把这些图像放在一个嵌入式设备上使用PIL在python中旋转并将expand参数设置为true时指定图像填充颜色,python,image,colors,python-imaging-library,rotation,Python,Image,Colors,Python Imaging Library,Rotation,我正在尝试使用PIL在Python中旋转一个图像,并将expand参数设置为true。看起来,当我的图像背景为黑色时,保存为bmp的结果图像将比我的图像背景为白色时小得多,然后我将黑色替换为白色。在任何一种情况下,我的原始图像总是两种颜色,现在我需要文件的大小要小,因为我把这些图像放在一个嵌入式设备上 如果我可以在展开时强制旋转以填充另一种颜色,或者如果有其他方法旋转图片以使其变小,您有什么想法?如果您的原始图像没有alpha层,您可以使用alpha层作为遮罩将背景转换为白色。当rotate创建
如果我可以在展开时强制旋转以填充另一种颜色,或者如果有其他方法旋转图片以使其变小,您有什么想法?如果您的原始图像没有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')
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()
这是一个工作版本,灵感来自于答案,但它不需要打开或保存图像就可以工作,并演示了如何旋转文本 这两幅图像的彩色背景和alpha通道不同于零,以显示发生了什么。将两个alpha通道从92更改为0将使它们完全透明
# 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')
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()
rotate
方法中有一个参数fillcolor
,用于指定将用于扩展区域的颜色:
white = (255,255,255)
pil_image.rotate(angle, PIL.Image.NEAREST, expand = 1, fillcolor = white)
为什么在
复合中使用了两次rot
?@Dims第三个参数仅使用rot
的alpha层作为掩码。仍然不理解。为什么不把旋转后的图像放在白色上?@Dims我不认为mask参数对于composite
是可选的。如果这是你的观点,我相信粘贴或其他方法也同样有效。应该注意的是,你需要枕头版本>5.0或类似的东西。否则错误:TypeError:rotate()获得了一个意外的关键字参数“fillcolor”
Pass“white”
作为字符串。它成功了,谢谢。