PIL python中的仿射变换 [英] affine transform in PIL python

查看:817
本文介绍了PIL python中的仿射变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PIL python库中的im.transform方法遇到问题.我以为我已经弄清楚了参数A到F的逻辑,但是,尽管波纹管功能计算出的所有四个角都具有正确的正值,但最终得到的图像却朝错误的方向旋转并被切除.

I have problems with the im.transform method in PIL python library. I thought I figured out the logic of parameters, A to F, however, the resulting image gets rotated in the wrong direction and cut off although all four corners calculated by the bellow function have correct positive values.

有人可以给我公式来计算两个坐标系中三个相同点的仿射参数(A到F)吗?

Could anybody give me formulas to calculate affine parameters (A to F) from three identical points in both coordinate systems?

def tran (x_pic, y_pic, A, B, C, D, E, F):
  X = A * x_pic + B * y_pic + C
  Y = D * x_pic + E * y_pic + F
  return X, Y

推荐答案

转换对我来说很好.作为示例,我们将绕着与(0,0)不同的中心旋转图像,并具有可选的缩放比例和平移到新的中心.这是使用transform的方法:

transform works fine for me. As an example we'll rotate an image around a center different from (0,0) with optional scaling and translation to a new center. Here is how to do it with transform:

def ScaleRotateTranslate(image, angle, center = None, new_center = None, scale = None,expand=False):
    if center is None:
        return image.rotate(angle)
    angle = -angle/180.0*math.pi
    nx,ny = x,y = center
    sx=sy=1.0
    if new_center:
        (nx,ny) = new_center
    if scale:
        (sx,sy) = scale
    cosine = math.cos(angle)
    sine = math.sin(angle)
    a = cosine/sx
    b = sine/sx
    c = x-nx*a-ny*b
    d = -sine/sy
    e = cosine/sy
    f = y-nx*d-ny*e
    return image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=Image.BICUBIC)

这篇关于PIL python中的仿射变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆