I have an image like this:

And want it to look something like this (just the inside fill):

Essentially I want to mark the beginning and end of the path and have the gradient follow that path. It's okay if I have to do this in two steps: one gradient in each direction (the desired output is a red along the narrow axis and green along the long axis).
Alternately, is there a way to morph the borders of an image? Then I could import a plain gradient and morph it's shape to match what I want.