如何在python3 Pillow中清除区域



如何在python3 Pillow中清除带有ImageDraw的区域?

im = Image.new('RGBA', (1200, 400), (0, 0, 0, 0))
draw = ImageDraw.Draw(im)
...
draw.rectangle((100, 100, 300, 150), fill=(0, 0, 0, 0))
draw.text((100, 100), 'Hello World!', font=font, fill=(0, 0, 255, 255))
canvas.write(im.tobytes())

我的想法是用fill=(0, 0, 0, 0)画一个矩形,但由于alpha组件0,这个矩形被混合到图像中,没有改变像素。

我每秒更新图像 60 次,但我负担不起重绘整个图像的费用。

该错误在我的代码中的其他地方。

要清除区域,只需致电

draw.rectangle((100, 100, 300, 150), fill=(0, 0, 0, 0))

区域(100, 100, 300, 150)将使用fill参数中给出的颜色填充。在这种情况下,矩形根本不混合。

最新更新