db.define_table('article',
Field("title"),
Field("article_text", "text"),
Field("picture", "upload"),
Field("thumbnail", "upload")
)
from images import THUMB
from smarthumb import SMARTHUMB
box = (200, 200),
db.article.thumbnail.compute = lambda row: SMARTHUMB(row.picture, box)
db.define_table('uploads',
Field('name','string'),
Field('mainfile','upload'),
Field('thumb','upload',writable=False,readable=False),
)
File "/home/mauricewaka/web2py/applications/bot/modules/smarthumb.py", line 21, in SMARTHUMB
while img.size[0] / factor > 2 * box[0] and img.size[1] * 2 / factor > 2 * box[1]:
TypeError: '>' not supported between instances of 'float' and 'tuple'
My path is:
img = Image.open(request.folder + '/uploads/' + image)
box = (200, 200),
should
box = (200, 200)
# -*- coding: utf-8 -*-import osfrom random import seed
from random import random
try: from PIL import Imageexcept: 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