PIL python中的仿射变换

Mat*_*tej 4 affinetransform python-imaging-library

我在PIL python库中遇到了im.transform方法的问题.我认为我弄清了参数A到F的逻辑,然而,尽管由波纹管函数计算的所有四个角都具有正确的正值,但是所得到的图像在错误的方向上旋转并被切断.

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

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
Run Code Online (Sandbox Code Playgroud)

byt*_*ish 9

变换对我来说很好.作为一个例子,我们将围绕一个不同于(0,0)的中心旋转图像,并可选择缩放和平移到新的中心.以下是如何使用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)
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.如果没有,请告诉我.

干杯,菲利普.