Idea... Thumbnailing...

38 views
Skip to first unread message

Jason Brower

unread,
Jun 27, 2009, 6:17:13 AM6/27/09
to Web2py Mailing List
I wonder if there is a nice way to implement thumbnailing. I am working
with a lot of images in my website and it would be nice to have a way to
scale them before they get to the user. That way my big images scale to
the need of the view.
Regards,
Jason Brower


Joe Barnhart

unread,
Jun 27, 2009, 5:39:14 PM6/27/09
to web2py Web Framework
There is the Python Imaging Library (PIL) that lets you open and
manipulate images from Python applications. Maybe it would be useful
in this context?

(I'm getting "bad gateway" errors trying to pull up the PIL website at
this moment. Hopefully a problem only for my geography.)

Tim Michelsen

unread,
Jun 28, 2009, 10:21:25 AM6/28/09
to web...@googlegroups.com
> There is the Python Imaging Library (PIL) that lets you open and
> manipulate images from Python applications. Maybe it would be useful
> in this context?
many PHP projects use gd for such purposes.
It is normally supported on apache webservers.

Here is a python interface:
http://newcenturycomputers.net/projects/gdmodule.html

Jason Brower

unread,
Jun 28, 2009, 11:57:50 AM6/28/09
to web...@googlegroups.com
I have thought about using imagemagick something I have grown used to.
Let's see if I can't implement my ideas with PIL.
Regards,
Jason

Francisco Gama

unread,
Jun 28, 2009, 5:40:00 PM6/28/09
to web2py Web Framework
tip:
if you use a Mac, try the tool Picturesque.

There are many tools that can do batch resizing on images and adding
suffixes like this. I think gimp also does it.

Jason Brower

unread,
Jun 28, 2009, 11:04:03 PM6/28/09
to web...@googlegroups.com
Yeah, I could use gimp, but I was hoping on a system where it would
create a thumb for me. That way when I use an image it would have a
perameter I could send to it to tell it to resize before sending to
server.
Regards,
Jason

mdipierro

unread,
Jun 28, 2009, 11:36:37 PM6/28/09
to web2py Web Framework
I think imagemagic is the best way. It is in every unix system and it
very easy to use form the shell. You could even make your own
validator that calls os.system('convert ....') on upload.

Massimo

Yarko Tymciurak

unread,
Jun 28, 2009, 11:52:30 PM6/28/09
to web...@googlegroups.com
the gd library code looks stagnant since 2007... is this correct?

cjparsons

unread,
Jun 29, 2009, 11:39:43 AM6/29/09
to web2py Web Framework
I've had success with PIL to thumbnail uploaded images.

In upload form handling:

if form.accepts(request.vars,session):
if not form.vars.get("delete_this_record",False):
response.flash = "image accepted"
makeThumbnail(form.vars.id)
else:
response.flash = "image deleted"

then the function to make the thumbnail. This might have less error
handling than you'd like. My images are in the db, the main image is
field 'image_blob' and the thumbnail, also a blob, 'thumbnail_data'.
If yours are in a file then the changes you'd need would actually make
the code simpler.

THUMBNAILSIZE = 120

def makeThumbnail(id):
import Image
import StringIO

imgRecord = db.images[id]
if not imgRecord:
return None
imgData = Image.open(StringIO.StringIO
(imgRecord.image_blob)).convert("RGB")
imgData.thumbnail((THUMBNAILSIZE,THUMBNAILSIZE),Image.ANTIALIAS)
thumbFile = StringIO.StringIO()
imgData.save(thumbFile,"JPEG")
thumbFile.seek(0)
imgRecord.update_record(thumbnail_data=thumbFile.read())
db.commit()
return True

From memory, the above will shrink the picture so the largest
dimension is THUMBNAILSIZE, and the aspect ratio is maintained - the
shorter dimension may be less than THUMBNAILSIZE if the image wasn't
square.

From a different iteration of my code, the following will always give
you a square of dimensions xy by xy, without distorting the image, but
will crop the image if it wasn't already square.

def makeThumbnail(blob, xy):
imgData = Image.open(StringIO.StringIO(blob)).convert("RGB")
# make sure thumbnails are square by cropping original to a
# square.
# Find the longest side
w,h = imgData.size
if w >= h:
voffset=0
hoffset=(w-h)/2
ex = h
else:
hoffset=0
voffset=(h-w)/2
ex = w
imgData = imgData.transform((xy,xy),Image.EXTENT,
(hoffset,voffset,hoffset+ex,voffset+ex),Image.BICUBIC)
thumbFile = StringIO.StringIO()
imgData.save(thumbFile,"JPEG")
thumbFile.seek(0)
return thumbFile

Sorry if the speed I'm writing my reply, or the inelegance of my
implementation makes this post less than helpful.

Chris



mdipierro

unread,
Jun 29, 2009, 12:12:29 PM6/29/09
to web2py Web Framework
Something like this could go in gluon.conrtib send me a patch when you
have time.

Jason Brower

unread,
Jun 29, 2009, 1:07:09 PM6/29/09
to web...@googlegroups.com
Or better, why don't we just package this with web2py?
http://www.imagemagick.org/download/python/
That would be fancy schmancy.
Regards,
Jason Brower

Jason Brower

unread,
Jun 29, 2009, 1:11:38 PM6/29/09
to web...@googlegroups.com
As nice as this is, the real power, I think, would be in requesting an
image, declaring it's width and height, and it resizes or sends an image
read for you. At least at first thought, it would be cool.
Regards,
Jason

mdipierro

unread,
Jun 29, 2009, 2:14:30 PM6/29/09
to web2py Web Framework
It should be easy to create a validator that does that and calls
'imagemagic convert', validators are filters. All you need to do is
read the tream, save it in a tmp file, convert the tmp file, read the
database back in the stream, rewind the stream for further processing.

Joe Barnhart

unread,
Jun 29, 2009, 2:23:23 PM6/29/09
to web2py Web Framework
Since PIL is all Python, do you suppose it's compatible with GAE?
That could give it a nod over ImageMagick in my mind.

mdipierro

unread,
Jun 29, 2009, 2:31:40 PM6/29/09
to web2py Web Framework
It is not pure python it is a binary library. As far as I know none of
them runs on GAE.

Hans Donner

unread,
Jun 29, 2009, 3:02:48 PM6/29/09
to web...@googlegroups.com

Hans Donner

unread,
Jun 29, 2009, 3:03:45 PM6/29/09
to web...@googlegroups.com
Note: In order to use the Images API in your local environment you
must first download and install PIL, the Python Imaging Library. PIL
is not available on App Engine; it is only used as a stub for the
Images API in your local environment. Only the transforms provided in
the images API are available on App Engine.

Hans Donner

unread,
Jun 29, 2009, 3:04:25 PM6/29/09
to web...@googlegroups.com
Sounds like we need an IAL (image abstraction layer) besides the DAL

Jason Brower

unread,
Jun 29, 2009, 3:17:43 PM6/29/09
to web...@googlegroups.com
Oh yeah, forgot about Google Apps Engine.
Reply all
Reply to author
Forward
0 new messages