Affine transform in PIL Python

前端 未结 1 539
花落未央
花落未央 2021-02-02 04:22

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 wro

1条回答
  •  北海茫月
    2021-02-02 05:03

    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)
    

    0 讨论(0)
提交回复
热议问题