I had trouble with the above code. Perhaps it was because i am using the 
uploadseparate =true parameter which means images get dropped into 
different folders. Or perhaps because i don't know what  i am doing.

Anyhow, the code below works for me. I had to manipulate the thumbnail 
filename a lot to get it to work. There must be a better way than this 
though...

# -*- coding: utf-8 -*-
import os
from random import seed
from random import random

try:
    from PIL import Image
except:
    import Image

db.define_table(
    'Media',
    Field('Description'),
    Field('FileReference', 'upload', uploadseparate=True, 
requires=IS_NOT_EMPTY() and IS_IMAGE(extensions=('jpeg', 
'png','jpg','tif'))),
    Field('ThumbnailLocation', type='upload', compute=lambda row: 
SMARTHUMB(row.FileReference, (100,100))),
    format = '%(Description)s')

def SMARTHUMB(FileReference, box, fit=True, name="thumb"):
    '''Downsample the image.
     @param img: Image -  an Image-object
     @param box: tuple(x, y) - the bounding box of the result image
     @param fit: boolean - crop the image to fill the box
    '''
    
    img_location = GetFullFilename('', FileReference)
    img = Image.open(img_location)
    
    #preresize image with factor 2, 4, 8 and fast algorithm
    factor = 1
    while img.size[0] / factor > 2 * box[0] and img.size[1] * 2 / factor > 
2 * box[1]:
        factor *= 2
    if factor > 1:
        img.thumbnail((img.size[0] / factor, img.size[1] / factor), 
Image.NEAREST)

    #calculate the cropping box and get the cropped part
    if fit:
        x1 = y1 = 0
        x2, y2 = img.size
        wRatio = 1.0 * x2 / box[0]
        hRatio = 1.0 * y2 / box[1]
        if hRatio > wRatio:
            y1 = int(y2 / 2 - box[1] * wRatio / 2)
            y2 = int(y2 / 2 + box[1] * wRatio / 2)
        else:
            x1 = int(x2 / 2 - box[0] * hRatio / 2)
            x2 = int(x2 / 2 + box[0] * hRatio / 2)
        img = img.crop((x1, y1, x2, y2))

    #Resize the image with best quality algorithm ANTI-ALIAS
    img.thumbnail(box, Image.ANTIALIAS)

    #Assume five parts to the filename split on "." eg 
Media.FileReference.a677042dfcd329d8.6361742e6a7067.jpg
    #The 4th part seems to effect the filename and extension
    fn_parts = FileReference.split(".")
    seed(1)
    thumb_filename = fn_parts[0] + '.' + fn_parts[1] + '.' + 
fn_parts[2][0:2] + str(random())[2:16]  + '.' + fn_parts[3] + '.' + 
fn_parts[4]

    img_location = GetFullFilename('', thumb_filename)
    img.save(img_location)
    
    return thumb_filename

def GetFullFilename(NamePrefix, FileReference):
    folder = FileReference[20:22] # The first two characters of the 3rd 
part of the name
    FileLocation = request.folder + '\\uploads\\Media.FileReference\\' + 
folder + '\\' + NamePrefix + FileReference
    return FileLocation


 

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/a95964bd-555f-4f43-bba1-0df635336d0c%40googlegroups.com.

Reply via email to