2012-10-17 38 views
1

我使用GAE上傳圖片,並使用google.appengine.api.images來調整尺寸。但是我想獲取這張圖片的原始尺寸upload.i嘗試使用image.width和image.height.but得到int不可調用error.soi感謝任何幫助。如何獲取GAE上傳的圖片尺寸

請參閱下面的代碼:你看image_.size

class PictureUploader(webapp.RequestHandler): 
    WANNA_RATIO_ = 0.833333 # icon width : height | 100 : 120 
    MAX_RATIO_ = 0.913043 # 105 : 115 
    MIN_RATIO_ = 0.76 # 95 : 125 
    def get(self): 
     self.post() 

    def post(self): 
     if not check_session(self): 
      self.redirect('login', True) 

     from google.appengine.api import images 
     image_ = images.Image(self.request.get('picture')) 

     from decimal import Decimal 
     i_width = Decimal(image_.size[0]) 
     i_height = Decimal(image_.size[1]) 
     w_h_ratio = i_width/i_height 
     if PictureUploader.MIN_RATIO_ > w_h_ratio: # picture too narrow 
      h_ratio = i_height/120 
      expected_w = Decimal("%.6f"%(i_width/h_ratio)) 
      image_ = images.resize(image_,expected_w,120) 
     elif PictureUploader.MAX_RATIO_ < w_h_ratio: # picture too short 
      w_ratio = i_width/100 
      expected_h = Decimal("%.6f"%(i_height/w_ratio)) 
      image_ = images.resize(image_,100,expected_h) 
     else: 
      image_ = images.resize(image_, 100, 120) 

     models.Life(user = self.session['key'],pictureblob=image_).put() 

[0],這是行不通的。

+3

錯誤'int是不是callable'似乎表明您意外後放在()屬性名,即你有'image.height()'它應該只是'image.height' –

回答

1

使用

image.height 
image.width 

你所得到的錯誤意味着你在呼喚像那些屬性:

image.height() -> wrong 
image.width() -> wrong 
+0

抱歉,它仍然不起作用,我不知道我是否正確使用GAE API ,如果我完全錯了,請糾正我。讓我詳細說明問題,我不確定有兩點:1.看到這裏「image_ = images.Image(self.request.get('picture'))」,你可能知道GAE和圖片是'輸入類型=「文件」',所以我不知道可以像這樣創建圖像對象;如果Q1是正確的,我修正了像這樣的代碼「height = image _._ height width = image _._ width」,並且我使用反映了這些屬性是否存在(它們確實存在。),但是我得到了NoneType:None 。 –

+0

圖片上沒有'size'屬性。使用'image.width'和'image.height',你會得到你想要的值。我只是試了一下,它工作正常,但'AttributeError:'圖像'對象沒有屬性'大小'' – aschmid00

+0

是的。有用。哦,我忘了我是如何得到這種錯誤,現在我得到一個新的,當我把它放入數據存儲區,「RequestTooLargeError:API調用datastore_v3.Put()的請求太大。」發生。你可以評論這個或任何解決方案。 –