2008-11-05 116 views
15

如何檢查用戶上傳的文件是否爲Python(Google App Engine)中的真實jpg文件?Python:檢查上傳的文件是否爲jpg

這是多遠我得到了現在:

腳本通過HTML表單提交接收圖像,並通過下面的代碼

... 
incomming_image = self.request.get("img") 
image = db.Blob(incomming_image) 
... 

我發現mimetypes.guess_type處理,但它並不適用於工作我。

回答

36

如果您不僅需要查看擴展名,還有一種方法是讀取JPEG標頭,並檢查它是否與有效數據匹配。這種格式是:

Start Marker | JFIF Marker | Header Length | Identifier 
0xff, 0xd8 | 0xff, 0xe0 | 2-bytes | "JFIF\0" 

如此快速識別器將是:

def is_jpg(filename): 
    data = open(filename,'rb').read(11) 
    if data[:4] != '\xff\xd8\xff\xe0': return False 
    if data[6:] != 'JFIF\0': return False 
    return True 

但是這並不會捕獲任何錯誤的數據在體內。如果你想要一個更強大的支票,你可以嘗試加載它PIL。例如:

from PIL import Image 
def is_jpg(filename): 
    try: 
     i=Image.open(filename) 
     return i.format =='JPEG' 
    except IOError: 
     return False 
0

使用PIL。如果它可以打開文件,它是一個圖像。

從教程...

>>> import Image 
>>> im = Image.open("lena.ppm") 
>>> print im.format, im.size, im.mode 
+2

這不會在App Engine中的工作:PIL包含C代碼,因此無法使用。 Images API(http://code.google.com/appengine/docs/images/)使用PIL,但它已被刪除。 – chryss 2008-11-06 00:13:18

33

無需使用和安裝這個PIL lybrary,恰好有fited對於這種用法的imghdr標準模塊。

http://docs.python.org/library/imghdr.html

import imghdr 

image_type = imghdr.what(filename) 
if not image_type: 
    print "error" 
else: 
    print image_type 

因爲你已經從一個流的圖像,你可以很可能使用的流選項是這樣的:

image_type = imghdr.what(filename, incomming_image) 

Actualy這對我的作品在塔(甚至如果我還沒有完成一切): 在Mako模板中:

${h.form(h.url_for(action="save_image"), multipart=True)} 
Upload file: ${h.file("upload_file")} <br /> 
${h.submit("Submit", "Submit")} 
${h.end_form()} 

在上傳CONTROLER:

def save_image(self): 
    upload_file = request.POST["upload_file"] 
    image_type = imghdr.what(upload_file.filename, upload_file.value) 
    if not image_type: 
     return "error" 
    else: 
     return image_type 
+3

+1作爲標準模塊imghdr – 2014-11-19 17:32:36

1

更普遍的解決方案是使用Python結合到Unix「文件」命令。爲此,請安裝python-magic軟件包。例如:

import magic 

ms = magic.open(magic.MAGIC_NONE) 
ms.load() 
type = ms.file("/path/to/some/file") 
print type 

f = file("/path/to/some/file", "r") 
buffer = f.read(4096) 
f.close() 

type = ms.buffer(buffer) 
print type 

ms.close() 
0

JPEG文件規範的最後一個字節似乎超出了e0。捕獲前三個是啓發式簽名的'夠用',以可靠地識別文件是否是jpeg。請參考下面的建議修改:

def is_jpg(filename): 
    data = open("uploads/" + filename,'rb').read(11) 
    if (data[:3] == "\xff\xd8\xff"): 
     return True 
    elif (data[6:] == 'JFIF\0'): 
     return True 
    else: 
     return False 
相關問題