Is it possible to mask an image in Python Imaging Library (PIL)?

对着背影说爱祢 提交于 2019-12-13 00:31:28

问题


I have some traffic camera images, and I want to extract only the pixels on the road. I have used remote sensing software before where one could specify an operation like

img1 * img2 = img3

where img1 is the original image and img2 is a straight black-and-white mask. Essentially, the white parts of the image would evaluate to

img1 * 1 = img3

and the black parts would evaluate to

img1 * 0 = img3

And so one could take a slice of the image and let all of the non-important areas go to black.

Is there a way to do this using PIL? I can't find anything similar to image algebra like I'm used to seeing. I have experimented with the blend function but that just fades them together. I've read up a bit on numpy and it seems like it might be capable of it but I'd like to know for sure that there is no straightforward way of doing it in PIL before I go diving in.

Thank you.


回答1:


The Image.composite method can do what you want. The first image should be a constant value representing the masked-off areas, and the second should be the original image - the third is the mask.



来源:https://stackoverflow.com/questions/28358379/is-it-possible-to-mask-an-image-in-python-imaging-library-pil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!