How to create a transparent gradient along the edge of an image/mask image with python?

强颜欢笑 提交于 2020-06-29 03:59:24

问题


Basically I want to create of a halo effect for the image that fades to transparent.

I have images of wounds (and their masks), with no background. I want to add a red gradient behind it, that way if I paste it on something it looks like the area surrounded the wound is irritated.

image and mask ...

Originally, I was making a radial (circle) gradient and pasted my transparent image on it, but this does not look good for my non circular images.

image with circle gradient

same image as above,but pasted on white easier to see gradient

...

Now, i think the best idea would be to make the gradient in a shape the goes around the edge of the image to a certain distance and fades. something like this ( i did this poorly with paint ... but basically for it to go out a certain distance, from the edge of the object, and fade out)

paint version of what i would like to do ...

My code for the the circle gradient is posted below, this creates a transparent circle gradient. Once i have this i paste my transparent image on it. I do not know how to change this code so that it creates the gradient the edge of the image.

import numpy as np, 
from PIL import Image

W,H=900,900
im = Image.new(mode='RGB', size=(W,H), color=(153,0,0))
Y = np.linspace(-1, 1, H)[None, :]*255
X = np.linspace(-1, 1, W)[:, None]*255
alpha = np.sqrt(X**2 + Y**2) # equation of a circle 
alpha = 255 - np.clip(0,255,alpha)

Please help

来源:https://stackoverflow.com/questions/62456912/how-to-create-a-transparent-gradient-along-the-edge-of-an-image-mask-image-with

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