2015-06-26 150 views
0

我在這裏使用了此處的建議http://www.web2pyslices.com/slice/show/1387/upload-image-and-make-a-thumbnail 來製作圖像的縮略圖。 我有縮略圖,但我無法顯示它。Image Web2py中的縮略圖:無法顯示縮略圖

以下是我的函數: db.py:

db.define_table('uploads', Field('dataset', 'reference dataset'), 

Field('filename', represent = lambda x, row: "None" if x == None else [:45]), 

Field('image', 'upload', uploadseparate=True, requires=IS_NOT_EMPTY() and IS_IMAGE(extensions=('jpeg', 'png','jpg','tif'))), 
Field('thumb', 'upload', uploadseparate=True, requires=IS_NOT_EMPTY() and IS_IMAGE(extensions=('jpeg', 'png', 'jpg', 'tif')))) 

default.py:

def makeThumbnail(dbtable,ImageID,size=(150,150)): 
    try: 
     thisImage=db(dbtable.id==ImageID).select()[0] 
     import os, uuid 
     from PIL import Image 
    except: return 
    im=Image.open(request.folder + 'uploads/' + thisImage.image) 
    im.thumbnail(size,Image.ANTIALIAS) 
    thumbName='uploads.thumb.%s.jpg' % (uuid.uuid4()) 
    im.save(request.folder + 'uploads/' + thumbName,'jpeg') 
    thisImage.update_record(thumb=thumbName) 
    return 


def insertImage(): 
    response.menu = [ 
    (T('Home'),False,URL('default','experimenter')), 
    (T('Manage Data Set'),False,URL('default','MDS')), 
    (T('Manage Experiment'),False,URL('default','ME')), 
    (T('Manage Workflow Element'),False,URL('default','MWE'))]   
    dbtable = db.uploads 
    record = None 
    record = db(db.dataset.id == request.args[0],ignore_common_filters=True).select().first() 
    form = FORM(dbtable, INPUT(_name='up_files', _type='file', 
    _multiple=True, requires=IS_NOT_EMPTY()),INPUT(_type='submit')) 
    # The multiple param lets us choose multiple files. 
    if form.process().accepted: 
    #onvalidation checks the uploaded files to make sure they are only txt,   config, or log. 
    makeThumbnail(dbtable,form.vars.id,(300,300)) 
    response.flash = 'files uploaded' 
    files = request.vars['up_files'] 
     if not isinstance(files, list): 
    #convert files to a list if they are not one already. 
     files = [files] 
     for file in files:   
     db.uploads.insert(dataset=record.id, filename=file.filename, image=db.uploads.image.store(file, file.filename)) 

     #store is a FIELD method that let's you save a file to disk. you can choose the directory if you want using the 'path' param. 
    else: 
     response.flash = 'Choose the Files you would like to upload' 
    return dict(form=form, record=record) 

然後在視圖:

{{extend 'layout.html'}} 
<h4>Manage Image of dataset: {{=record.name}}</h4> 

{{if images:}} 
<div style="overflow: auto;" width="80%"> 
<table> 
<tr> <th> Image </th> </tr> 
{{ 
    for image in images: 
    =TR(TD(image.filename), IMG(_src=URL('default', 'download', args=image.thumb)), A(str(T('View')),_href=URL("show", args=[image.id,rowId])), A(str(T('Delete')),_href=URL('deleteImage',args=image.id)))}} 
    {{pass}} 
</table> 

</div> 
{{pass}} 

注:我想在圖像列表中顯示每個圖像的縮略圖(請參閱視圖)。 我沒有得到縮略圖,而是取而代之的是小問號。 PS:我無法上傳圖片。 我想用圖像代替問號。我在insertImage()函數和視圖中做了一些錯誤。 在此先感謝您的幫助!

回答

1

首先,您似乎在混淆FORMSQLFORM。前者用於創建自定義表單(不與任何數據庫表連接),後者用於基於數據庫表構建表單(並因此自動處理插入)。您不能像在代碼中那樣將DAL表對象傳遞給FORM--它只是簡單地將Table對象序列化爲其字符串名稱,該名稱將包含在HTML表單DOM中,而不起作用。此外,在這種情況下,form.vars.id將只是NoneFORM不會生成記錄ID,因爲它不執行任何數據庫插入操作)。

此外,並非直接將文件保存在makeThumbnail中,更好的選擇是將圖像保存到StringIO對象,然後將該對象傳遞到db.uploads.thumbnail.store()(與存儲原始圖像相同)。在這種情況下,縮略圖字段的.store()方法將自動處理文件命名和保存。

from cStringIO import StringIO 
tmp = StringIO() 
im.save(tmp, 'jpeg') 
tmp.seek(0) 
thisImage.update_record(thumb=db.uploads.thumb.store(tmp, filename='thumbnail.jpg')) 

有關更多詳細信息,請參閱http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer