I'm wondering how to apply a variable mask to an image's transparency in Java (presumably using buffered image). So, for instance, I want to apply a mask such as the following:

To the an image such as the following:

In order to get:

It's important that the transparency is changed by the gradient, not just the color, because I want to be able to to this kind of thing:

I've found an incredibly slow way of achieving this by going through and setting the transparency value of every single pixel manually, but as I said, it's super slow. What should I be looking at to try to accomplish this? Or should I even be doing it in Java? I've been getting to know python recently, so if anyone thinks it's better to try this sort of thing with a particular python module, that would be useful information as well.