python image thumbnail generator?

Mike C. Fletcher mcfletch at rogers.com
Sat Aug 27 23:09:43 EDT 2005


Chris Dewin wrote:

>Hi. I run a website for my band, and the other guys want an image gallery.
>
>I'm thinking it would be nice and easy, if we could just upload a jpg into
>a dir called "gallery/". When the client clicks the "gallery" link, a 
>cgi script could search the gallery/ dir, and create thumbnails of any
>jpeg images that don't already have a thumbnail associated with them. The
>script could then generate a page of clickable thumbnails.
>
>A few questions:
>
>(1) Can this be done with python? If so, what module do I need to look up?
>  
>
PIL can certainly handle the functionality.

>(2) If it can't be done with python, is there some small utility that will
>do it for me? Something I could easily install locally in my own
>"public_html" dir on my website?
>  
>
The core function looks something like this:

import Image # this is PIL

def getThumbnail( filename, size = (32,32) ):
    '''Get a thumbnail image of filename'''
    image = Image.open(filename)
    rx, ry = image.size[0]/float(size[0]), image.size[1]/float(size[1])
    if rx > ry:
        resize = int(size[0]), int(round(image.size[1]*(1.0/rx), 0))
    else:
        resize = int(round(image.size[0]*(1.0/ry), 0)), int(size[1])
    image = image.resize( resize, Image.BILINEAR )
    newimage = Image.new( 'RGB', size, )
    x,y = image.size
    newimage.paste(
        image, ((size[0]-x)/2, (size[1]-y)/2),
    )
    return newimage

then you call newImage.save( filename, format ) to save the result.

>(3) Is this the sort of thing which, if done regularly, would hog far far
>too much of my webhosts system resources?
>  
>
As long as you *only* generate images for sources that don't yet have 
(or are newer than) their thumbnail you should be fine.

>(4) Am I talking about re inventing the wheel?
>  
>
Probably.  ZPhotoSlides (a Zope product) provides thumnailing, 
slideshows and the like, probably closer to what you'd want for this 
kind of application.  You'll probably find other non-Zope versions as well.

Good luck,
Mike

-- 
________________________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com




More information about the Python-list mailing list