[Image-SIG] Image warping in Python or PIL
Fredrik Lundh
fredrik at pythonware.com
Sat May 8 13:49:14 CEST 2010
On Sat, May 8, 2010 at 5:58 AM, Son Hua <songuke at gmail.com> wrote:
> Hi,
>
> Can anybody show me some hints on how to implement an image warping function
> efficiently in Python or PIL? Suppose we have a function f(x, y) -> (x', y')
> that warps a pixel location (x, y) to (x', y'). Because (x', y') may end up
> to be non-integer, and many (x, y) can map to the same (x', y'), reverse
> mapping is used. That is at every destination pixel (x', y'), we go and find
> the original pixel location (x, y) by taking (x, y) = f_1(x', y'), where f_1
> is the inverse function of f.
>
> Suppose the inverse function f_1 is given. So, for each pixel in the
> destination image, we can map to a non-integer pixel in the source image.
> Therefore, we must bilinear interpolate at the source location for the
> color.
The transform(MESH) operation might be what you need.
http://effbot.org/tag/PIL.Image.Image.transform
Instead of mapping between individual pixels, cover the output image
with a grid, and calculate the source pixels for the corners of each
grid rectangle (forming a quadrilateral area). Pass in the resulting
list of (grid rectangle, quadrilateral) pairs to transform(MESH), and
it'll take care of the rest.
(you can use a generator to produce mesh pairs on the fly)
The result is (usually) an approximation, with the maximum error
controlled by the grid resolution.
</F>
More information about the Image-SIG
mailing list