PIL Image transform

Dean Card mailer at forums.com
Fri Aug 11 14:14:26 EDT 2006


> 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;
> }
>
> 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?


thanks for the reply.  I have been able to use the Image.PERSPECTIVE 
transform via trial and error to get it to work properly for each transform. 
What I am really looking for I guess is a way to calculate the 8 int tuple 
to match the perspective change I am going for.  For a given image there may 
be 5 elements that need to be 'painted' on with perspective.  A database 
table will include the transform tuples based on the source image.  So, by 
passing a starting image and a pattern image, the starting image can be 
covered with.  Perhaps the best way to explain is visually....

http://seanberry.com/perspective.png

What I need to know is how you take a surface like (P1, P5, P6, P2) and 
describe it with the 8 int tuple?

I know that for the elements in the transform a - h they are as follows...
(a, b, c, d, e, f, g, h)
a / e is the ratio of height to width.  For a = 2, e = 1 the output is half 
the width of the original.
b is the tan of the angle of horizonal skew.  e is the vertical skew 
equivalent of b.
c and f are the x and y offsets respectively.
g and h are the values that actually distort the image ranther than doing an 
affine transform... which is where I need the help...

I appreciate any additional insight into this problem.

This is a small step in a massive project I am working on and need to get 
past this part to move on to the next.

I am also willing to $pay$ for help that results in a success.

Thanks.





More information about the Python-list mailing list