2011-06-03 37 views
1

我試圖從HTML窗體的輸入控件中截取圖像,以便在服務器端處理它之前將其轉換爲字節字符串。如何在上傳前獲取圖像的字符串表示形式?

如何攔截文件?

upload_files = self.get_uploads('file') 
# Intercept here to do something different than just upload 
blob_info = upload_files[0] 

如何將其轉換爲字節串,可轉換回後的圖像?

我正在使用Python和App Engine。

+2

base64? http://docs.python.org/library/base64.html – tMC 2011-06-03 05:00:30

+3

目前還不清楚你要問什麼 - 你引用的代碼是服務器端,所以當你運行它時,圖像已經被上傳。而且,image是一個字節字符串。 – 2011-06-03 05:20:48

+0

你已經有了一個字節串...你只需要將它存儲在一個blob屬性中。 – 2011-06-03 07:09:53

回答

0

假設你上傳控件是在一個名爲「圖像」的形式,並且使用Werkzeug's FileStorage

img_stream = self.form.image.data 
mimetype = img_stream.content_type 
img_str = img_stream.read().encode('base64').replace('\n', '') 

data_uri = 'data:%s;%s,%s' % (mimetype, 'base64', img_str) 

data_uri現在包含您需要的字符串信息。

謝謝大家的寶貴意見!

相關問題