Image upload error when using smarthumb

71 views
Skip to first unread message

Maurice Waka

unread,
Oct 3, 2019, 9:19:51 AM10/3/19
to web2py-users
I followed this example with this code
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),
   
)

I get this error:


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)


Massimo Di Pierro

unread,
Oct 4, 2019, 12:54:17 AM10/4/19
to web2py-users
box = (200, 200),

should

box = (200, 200)

(remove the extra comma :-))

Andrew Rogers

unread,
Mar 5, 2020, 10:55:26 AM3/5/20
to web2py-users
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


 
Reply all
Reply to author
Forward
0 new messages