使用PIL旋转时如何保持图像质量 [英] How to preserve Image Quality when rotating with PIL

查看:108
本文介绍了使用PIL旋转时如何保持图像质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Basically I'm trying to be able to rotate images via user interface, however I've noticed that the image quality severely decreases upon each rotation. Does anyone know how to fix that? Also when the image is rotated it crops off parts of the image each time.

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

here are some pictures of before and after: 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)

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

This tells it to use the highest quality interpolation algorithm that it has available, and to expand the image to encompass the full rotated size instead of cropping. The documentation does not say what color the background will be filled with.

这篇关于使用PIL旋转时如何保持图像质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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