Hello,

I would like to know how I can get the folder name on uploadseparate=True 
in order to pass it to the to the thumbnail generating function base on 
Bruno slice : 
http://www.web2pyslices.com/slice/show/1522/generate-a-thumbnail-that-fits-in-a-box

We already found a workaround with os.walk, but with greate amount of 
folder it will come slow with time :

#!/usr/bin/env python
# coding: utf8

from gluon import current
import os
try:
    from PIL import Image
except:
    import Image

def SMARTHUMB(image, 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
    '''
    if image:
        request = current.request
        image_dir = __find_image_dir__(image, os.path.join(request.folder, 
'uploads'))
        img = Image.open(os.path.join(image_dir, image))
        #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)

        root, ext = os.path.splitext(image)
        thumb = '%s_%s%s' % (root, name, ext)
        img.save(os.path.join(image_dir,thumb))
    return thumb

def __find_image_dir__(name, path):
    for root, dirs, files in os.walk(path):
        if name in files:
            return root

Thanks

Richard

-- 

--- 
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to