2013-07-23 69 views
3

基本上我試圖通過用戶界面來旋轉圖像,但是我注意到每次旋轉後圖像質量嚴重下降。有誰知道如何解決這個問題?同樣,當圖像旋轉時,它會每次裁剪圖像的一部分。如何在使用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') 

回答

4
rotated_small = photo_small.rotate(angle, resample=Image.BICUBIC, expand=True) 

這告訴它使用的是它具有最高品質的插值算法,並擴大圖像以包含完整的旋轉大小而不是裁剪。文檔沒有說明背景將填充什麼顏色。

+0

所以我認爲,而不是filter = Image.BICUBIC,它應該是resample = Image.BICUBIC。但這絕對有效!謝謝!! – loonyuni

+0

@ user2525507我在http://effbot.org/imagingbook/image.htm上使用的文檔似乎有誤。我會解決答案。 –

3

圖像是一個像素網格。如果旋轉它(並且角度不是90的倍數),旋轉後的網格必須重新匹配到新的非旋轉網格才能顯示圖像。在這個過程中無法避免一些損失。

唯一的選擇是將未旋轉的圖像保存在某處並總結多次旋轉的角度,並總是從最初未旋轉的圖像構建旋轉後的圖像。