简体   繁体   中英

Non-affine transformation with PIL

I have a rectangular image ( O ) and I want to wrap it in a circle ( I ). Basically I want to take the two cartesian axis x and y and map them to polar coordinates φ and r , so that I(φ,r) = O(f(φ),g(r)) being f and g linear functions.

I have found in PIL the Image.transform method, but as I read the documentation this only works with affine transformation matrices.

1. Can this "wrap a rectangle into a circle" be done with an affine transform? I fear not.

2. How else can I achieve this?

As according to martineau there is no such functionality in PIL, I had to implement it myself:

Being overlay the cartesian image and circle the polar image.

for x in range (800):
    for y in range (800):
        r = ( (x - 400) ** 2 + (y - 400) ** 2) ** .5
        phi = math.atan2 (float (y - 400), float (x - 400) )
        tx = int (phi * 1200.0 / 2.0 / math.pi + 300) % 1200
        ty = int ( (r - 100.0) * 350.0 / 250.0)
        if 100 < r < 350: circle.putpixel ( (x, y), overlay.getpixel ( (tx, ty) ) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM