PIL Image transform

Ben C spamspam at spam.eggs
Fri Aug 11 13:08:03 EDT 2006


On 2006-08-09, Dean Card <mailer at forums.com> wrote:
> Okay, so here is the situation.  I have need to do some on-the-fly image 
> creation.  I have everything working great except for the last part of it, 
> applying a perspective type transform to the image.  The transform will take 
> a rectangular 2D image and transform it to a 3D representation in 2D.
>
> Please see the link to see what I am trying to do:
> http://seanberry.com/transform.png
>
> I have PIL v1.15 installed which has a PERSPECTIVE transform, but it does 
> not appear to do what I want - or I can't figure out how to use it correctly 
> because it is using a transform matrix's coefficients.
>
> Here is the only info I could find on the usage:
> http://mail.python.org/pipermail/image-sig/2005-February/003198.html

This looks like a correct description of the sources:

In Image.py:

elif method == PERSPECTIVE:
    # change argument order to match implementation
    data = (data[2], data[0], data[1],
            data[5], data[3],
            data[4],
            data[6],
            data[7])

and then in Geometry.c:

static int
perspective_transform(double* xin, double* yin, int x, int y, void*
        data)
{
    double* a = (double*) data;
    double a0 = a[0]; double a1 = a[1]; double a2 = a[2];
    double a3 = a[3]; double a4 = a[4]; double a5 = a[5];
    double a6 = a[6]; double a7 = a[7];

    xin[0] = (a0 + a1*x + a2*y) / (a6*x + a7*y + 1);
    yin[0] = (a3 + a4*x + a5*y) / (a6*x + a7*y + 1);

    return 1;
}

> This is for the creation of images to be used in Flash.  Originally I was 
> doing the image processing in Flash because Flash 8 has a BitmapData class 
> which does the basics of images, copy, transform, etc.  To accomplish the 
> transform I was using an algorithm that approximated triangles to fill and 
> worked really well, but I need the image processing to be server side, not 
> client.
>
> So, here I am.  Anyone have any idea how to accomplish my goal here?  Is 
> there a way to fill a triangle with a bitmap using PIL?  What about better 
> docs on the PERSPECTIVE transform?
>
> Thanks for any and all help on this.

Something like this is almost what you what:

im = im.transform(im.size, Image.PERSPECTIVE, (1, 0, 0, 0, 1, 0, -0.004, 0))

But the problem really is that the top row of the image is at at y of
0-- I think you want the origin of the image to be in the centre for
this to work properly.

Is there a way to do that in PIL?



More information about the Python-list mailing list