Python PIL image warping / Affine transformations
<p>Using Python PIL I want to transform input images in such a way that they seem to be in perspective. Most of the answers I have found are about rotating etc. The images below show one of the effects I am aiming at. Similarly I would like to do this not only from front to back, but also from left to right and vice versa. Another effect I would like is to warp the image in a way that the center is closer and the edges are more distant.</p>

<p>How should I call <code>im.transform</code> to get this effect? And why? </p>

<p>Input Image:</p>

<p><a href="http://i.stack.imgur.com/dHGcB.png" rel="nofollow"><img src="http://i.stack.imgur.com/dHGcB.png" alt="Input Image"></a></p>

<p>Output Effect:</p>

<p><a href="http://i.stack.imgur.com/GicNm.png" rel="nofollow"><img src="http://i.stack.imgur.com/GicNm.png" alt="Ouput Image"></a></p>

<p>I believe you are looking for <a href="https://en.wikipedia.org/wiki/3D_projection" rel="nofollow">perspective transformations</a>. You can do it with Pillow in following way:</p>

<pre><code>transformed = image.transform(
    image.size, Image.PERSPECTIVE,
    [
        a0, a1, a2, 
        a3, a4, a5,
        a6, a7
    ],
    Image.BILINEAR
)
</code></pre>

<p>Where a0-a7 is coefficients used for transformation in this way (from sources, Geometry.c):</p>

<pre><code>xout = (a0 * xin + a1 * yin + a2) / (a6 * xin + a7 * yin + 1);
yout = (a3 * xin + a4 * yin + a5) / (a6 * xin + a7 * yin + 1);
</code></pre>

python python-imaging-library