GAE 處理圖像相關的撇步

最近想完成一件聽起來不困難的功能

user 會指定一張FB 圖片, 而我們去將其資訊撈過來存起來, 包括圖檔本身

經過了一番摸索, 答案總是簡單的~

只要先 import images 這個檔案(from google.appengine.api)
再呼叫 images.Image(input 參數: 圖片串流資料) 即可產生一個image 實體

Note:圖片串流資料(binary) 在 python 中的型態為 str(bytestring)

例如: img = images.Image(blob_data)
這樣img 就是一個 Python Image 類別的物件, 可以去呼叫這個Class 底下的方法, 擁有這個Class 的property(詳參 參考文獻 2.)



注意:

Property data must be convertible to a Blob instance (Blob() argument should be str instance, not Image) , 也就是說不能直接拿Image 物件輸入Blob data 欄位(Google 的datastore 似乎不支援image 這樣的資料型態輸入)


如果要輸入Blobproperty 型態的欄位, 要在呼叫Image 相關函式時帶入output_encoding 參數, 例如:

resized_img = images.resize(self.request.get('avatar'), width=48, height=48, output_encoding=images.PNG)


參考文獻:
1. Images python API overview: https://developers.google.com/appengine/docs/python/images/overview
2. python image class : https://developers.google.com/appengine/docs/python/images/imageclass#Introduction
3. The Image Module: http://www.pythonware.com/library/pil/handbook/image.htm

留言

熱門文章