使用PIL旋转时如何保持图像质量


问题内容

基本上,我试图通过用户界面旋转图像,但是我注意到每次旋转图像质量都会严重下降。有谁知道该如何解决?同样,旋转图像时,每次都会裁切掉部分图像。

这是之前和之后的一些图片:http :
//imgur.com/a/QESKs

这是代码:

def onRotate(self):
    tanTheta = float(hh)/float(ww) 
    theta = math.atan(tanTheta) * 57.2957795 # convert to degrees
    if theta > 0:
        angle = (90 - theta) * -1
        clockwise = True
    elif theta < 0:
        angle = (270 - theta) * -1
        clockwise = False
    else:
        tkMessageBox('Angle not okay', 'Try again!')
    rotated_small = photo_small.rotate(angle) 
    rotated_small.save('small_rotate.jpg')
    self.load_imgfile('small_rotate.jpg')

问题答案:
rotated_small = photo_small.rotate(angle, resample=Image.BICUBIC, expand=True)

这告诉它使用可用的最高质量的插值算法,并扩展图像以包含完整的旋转尺寸,而不是裁切。该文档没有说明背景将填充什么颜色。