I can't display a thumbnail?

242 views
Skip to first unread message

BlueShadow

unread,
Sep 5, 2012, 7:35:26 AM9/5/12
to web...@googlegroups.com
Hi,
its driving me nuts. I used the code from web2pyslices to generate thumbnails:
db.py:
db
.define_table('Images',
   
Field('Name',length=512),
   
Field('Image','upload'),
   
Field('thumb','upload',writable=False,readable=False),
    format
= '%(Name)s' # <<< important
   
)
db
.define_table('Article',
   
Field('Title',length=512),
   
Field('Content','text'),
   
Field('Submitted','datetime',default=datetime.datetime.now()),
   
Field('Views','integer',default=0),
   
Field('TopImage',db.Images)
   
)
default.py:
def download():
   
return response.download(request, db)
def makeThumbnail(dbtable,ImageID,size=(200,200)):
   
try:
        thisImage
=db(dbtable.id==ImageID).select()[0]
       
import os, uuid
   
except:
       
print "Error while loading libraries"
       
return
   
try:
       
from PIL import Image
   
except:
       
print "Error while Importing PIL library"
       
return
   
print request.folder + 'uploads/' + thisImage.Image
    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)
    response
.flash = 'Thumb created everything went fine'
   
return
def newImage():
    dbtable
= db.Images          #uploads table name
   
if len(request.args):
        records
= db(dbtable.id==request.args[0]).select()
   
if len(request.args) and len(records):
        form
= SQLFORM(dbtable, records[0], deletable=True)
   
else:
        form
= SQLFORM(dbtable)
   
if form.accepts(request.vars, session):
        response
.flash = 'Entry for Images Database accepted,start creating thumb'
        makeThumbnail
(dbtable,form.vars.id,(200,200))
   
elif form.errors:
        response
.flash = 'Error in Form for Images Database'
   
## Quick list just to demonstrate...
    list
= crud.select(dbtable)
   
return dict(form=form,list=list)
def Article():
    id
=request.vars.id
    row
=db(db.Article.id==id).select()
    row
[0].update_record(Views=row[0].Views+1)
   
if len(row)==0:
        redirect
(URL(r=request,f='Articles'))
   
return dict(Article=row[0])
article.html
{{extend 'layout.html'}}
<h1> {{=Article.Title}} </h1>
<br>
<center>
{{print URL(r=request, c='default', f='download',args=Article.TopImage.thumb)}}
{{=IMG(_src=URL(r=request, c='default', f='download',args=Article.TopImage.thumb),_style="display:block;")}}
 </
center>
 
<br>
{{=XML(Article.Content)}}<br><br>
the thumbnail is generated by the code as it should. and it lands in the upload folder with the original image as it should. if I change the article.TopImage.thumb to .image it works perfectly. if I change it to .thumb again no image is displayed. the path and name I get from the print seem to be correct at least it is the same path as for the origianl image.
thanks for your help guys

villas

unread,
Sep 5, 2012, 10:07:29 AM9/5/12
to web...@googlegroups.com
def Article():
    id=request.vars.id
    row
=db(db.Article.id==id).select()

Not sure how you get the Images filenames.  The code does not seem join the Article to the Image?

Maybe something like:
def Article():
    id=request.vars.id  #  <<-- only ok for testing
    row
=db((
db.Article.id==id)&(db.Article.TopImage==db.Images.id)).select()



Regards, David

BlueShadow

unread,
Sep 5, 2012, 10:19:23 AM9/5/12
to
the image filename is joined to articles by this line:
Field('TopImage',db.Images)
id=request.vars.id  #<<-- only ok for testing
I removed some code to make it as short as possible the original version gets the article id from request.vars.id
if none is given it redirects to a page where you can choose the article you want to read.
completeArticle function:

Anthony

unread,
Sep 5, 2012, 10:38:23 AM9/5/12
to web...@googlegroups.com
On Wednesday, September 5, 2012 10:07:29 AM UTC-4, villas wrote:
def Article():
    id=request.vars.id
    row
=db(db.Article.id==id).select()

Not sure how you get the Images filenames.  The code does not seem join the Article to the Image?

Note, Article.TopImage.thumb does a recursive select, so it retrieves the value of the "thumb" field from the referenced db.Images record.

Anthony

Anthony

unread,
Sep 5, 2012, 10:59:35 AM9/5/12
to
    thumbName='uploads.thumb.%s.jpg' % (uuid.uuid4())
    im
.save(request.folder + 'uploads/' + thumbName,'jpeg')

The above is not quite the naming scheme expected by response.download. Instead of trying to make the name yourself, it might be easier to use the field's .store() method to handle it the naming and saving:

thisImage.update_record(thumb=db.Images.thumb.store(im, filename='thumbnail.jpg'))


Anthony

BlueShadow

unread,
Sep 6, 2012, 1:19:05 PM9/6/12
to web...@googlegroups.com
So I made an application containing just pa page to upload the image, make an article and display the article.
It shows the original picture perfectly. but the darn thumbnail not at all.
I didn't try to rename the picture since I got more than one in the original application and the uuid makes sure I got no problem adding a hundred pics.
And the name I get from printing the db entry also matches perfectly.
I hope someone finds what I did wrong.
Thanks
web2py.app.testthumb.w2p

Anthony

unread,
Sep 6, 2012, 3:53:22 PM9/6/12
to web...@googlegroups.com

BlueShadow

unread,
Sep 6, 2012, 4:23:50 PM9/6/12
to web...@googlegroups.com
the link directs to this group Anthony.
I didn't try:
thisImage.update_record(thumb=db.Images.thumb.store(im, filename='thumbnail.jpg'))
this would store the thumb directly in the database?

Anthony

unread,
Sep 6, 2012, 4:42:16 PM9/6/12
to web...@googlegroups.com
On Thursday, September 6, 2012 4:23:50 PM UTC-4, BlueShadow wrote:
the link directs to this group Anthony.

Yes, I know, but I noticed you're code didn't implement that solution so was wondering if you had tried it.
 
I didn't try:
thisImage.update_record(thumb=db.Images.thumb.store(im, filename='thumbnail.jpg'))
this would store the thumb directly in the database?

No, it will rename and store the file on the filesystem (assuming that's how the "thumb" field is defined) and store the new name in the "thumb" field, just as if you did an upload with SQLFORM.

Anthony
Message has been deleted

Paolo Caruccio

unread,
Sep 7, 2012, 10:14:35 AM9/7/12
to web...@googlegroups.com
You are storing in db.Images.thumb a text not an image (see attached picture)

To solve the issue, you could find interesting code here: https://gist.github.com/2639300
2012-09-07_161237.png

BlueShadow

unread,
Sep 7, 2012, 11:22:41 AM9/7/12
to web...@googlegroups.com
I tried your solution and it just leaves the thumb field empty and no thumbnail is created in the upload folder.
It can't be that difficult to get a thumbnail.
and how did you get this Database view I really like that^^
web2py.app.testthumb(1).w2p

Anthony

unread,
Sep 7, 2012, 11:38:15 AM9/7/12
to web...@googlegroups.com
On Friday, September 7, 2012 11:22:41 AM UTC-4, BlueShadow wrote:
I tried your solution and it just leaves the thumb field empty and no thumbnail is created in the upload folder.

Are you referring to Paolo's solution or this one:

thisImage.update_record(thumb=db.Images.thumb.store(im, filename='thumbnail.jpg'))

and how did you get this Database view I really like that^^

Are you talking about the image Paolo attached? That's just the Chrome developer tools (hit F12 in Chrome to open it).

Anthony

BlueShadow

unread,
Sep 7, 2012, 11:43:35 AM9/7/12
to
I was referring to paolos post
your solution gives me just the error message I don't understand Anthony. I posted the error message before but it got somehow deleted.

Anthony

unread,
Sep 7, 2012, 11:57:09 AM9/7/12
to web...@googlegroups.com
Yes, I see a message was deleted -- don't know who deleted it. Can you post the error again?


On Friday, September 7, 2012 11:41:05 AM UTC-4, BlueShadow wrote:
I was referring to paolos post
your solution gives me just the error message I don't understand Anthony. I posted the error message before but it got somehow deleted.

On Friday, September 7, 2012 5:38:15 PM UTC+2, Anthony wrote:

BlueShadow

unread,
Sep 7, 2012, 12:07:27 PM9/7/12
to web...@googlegroups.com
Traceback (most recent call last):
File "F:\Website\web2py\gluon\restricted.py", line 209, in restricted
exec ccode in environment
File "F:/Website/web2py/applications/testthumb/controllers/default.py", line 128, in <module>
File "F:\Website\web2py\gluon\globals.py", line 185, in <lambda>
self._caller = lambda f: f()
File "F:/Website/web2py/applications/testthumb/controllers/default.py", line 121, in newImage
makeThumbnail(dbtable,form.vars.id,(200,200))
File "F:/Website/web2py/applications/testthumb/controllers/default.py", line 106, in makeThumbnail
thisImage.update_record(thumb=db.Images.thumb.store(im, filename='thumbnail.jpg'))
File "F:\Website\web2py\gluon\dal.py", line 8442, in store
shutil.copyfileobj(file, dest_file)
File "C:\Python27\lib\shutil.py", line 48, in copyfileobj
buf = fsrc.read(length)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 512, in __getattr__
raise AttributeError(name)
AttributeError: read


that was the error message

Anthony

unread,
Sep 7, 2012, 1:24:33 PM9/7/12
to web...@googlegroups.com
How about if you do something like:

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

Anthony

BlueShadow

unread,
Sep 7, 2012, 4:24:46 PM9/7/12
to web...@googlegroups.com
Anthony you are the best. I got pretty much no Idea what your code does but it works perfectly. for all people who have the smae problem I posted the complete working app.

regards BlueShadow
web2py.app.testthumb.w2p

Anthony

unread,
Sep 7, 2012, 4:39:04 PM9/7/12
to web...@googlegroups.com
cStringIO.StringIO is a file-like object. The code "saves" the thumbnail to that object and then passes it to the .store() method, which treats it like an actual file.

Anthony
Reply all
Reply to author
Forward
0 new messages