简体   繁体   English

有没有办法更改 Python Imaging Library (PIL) 的默认填充颜色?

[英]Is there a way to change the Python Imaging Library (PIL)'s default fill color?

I'm currently using Python Imaging Library's (PIL) transform with EXTENT function to allow users to do some basic editing of images ie simple zoom in / zoom out, x and y-axis offsets, set background color, rotation etc我目前正在使用带有 EXTENT 函数的 Python 成像库 (PIL) 变换,以允许用户对图像进行一些基本编辑,即简单的放大/缩小、x 和 y 轴偏移、设置背景颜色、旋转等

And so one problem is that they are able to zoom out or offset enough so that parts of the final output image goes beyond the bounds of the source image.因此,一个问题是它们能够充分缩小或偏移,从而使最终输出图像的一部分超出源图像的边界。 When this happens, PIL fills this portion with a black color发生这种情况时,PIL 会用黑色填充此部分

Does anybody know if there's a way to set a custom fill color rather than the black default or has any suggestions on ways to get around this?有没有人知道是否有办法设置自定义填充颜色而不是黑色默认值,或者对解决此问题的方法有任何建议? Much appreciated非常感激

I was originally thinking of pre-pasting an alpha layer to where the bounds are exceeded but this seems to get complicated quite quickly..我原本想在超出边界的地方预先粘贴一个 alpha 层,但这似乎很快就变得复杂了..

[Edit] Maybe this will help. [编辑] 也许会有所帮助。 So, do something like (don't pay too much attention to the exact integers ) ...所以,做一些类似的事情(不要太关注确切的整数)......

image2 = image1.transform((600, 400), Image.EXTENT, (0, 0, 1200, 800))

draw = ImageDraw.Draw(image2)

# Fill in rectangle below the real image
draw.rectangle( (0, 50, 1200, 600), fill=(255, 150, 0))

# Fill in rectangle to the right of the real image
draw.rectangle( (100, 0, 800, 50), fill=(150, 255, 0))
del draw

image2.save(SAVE_NAME)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM