No such file or directory UPLOAD

69 views
Skip to first unread message

Gaël Princivalle

unread,
Nov 1, 2020, 2:18:49 PM11/1/20
to web2py-users

Hello.

I've migrated 3 Web2py apps from Webfaction to Opalstack.
For all these 3 apps I've the same ticket.

When I save a table record with an upload field Web2py tried to save in the myappupload folder instead of the myapp/upload folder.

Of course Web2py is unable to find this folder.

Someone knows why?
Could it be because Web2py 2.20.4 is not compatible with Python 2.7.5, so I must update Python to 2.7.16 or the last one 2.7.18?
Or perhaps it's a PIL problem?
from PIL import Image

userweb2py™     Version 2.20.4-stable+timestamp.2020.05.03.05.18.50
Python     Python 2.7.5: /home/user/apps/web2py_folder/env/bin/uwsgi (prefix: /home/user/apps/web2py_folder/env)
Traceback

Traceback (most recent call last):
  File "/home/user/apps/web2py_folder/web2py/gluon/restricted.py", line 219, in restricted
    exec(ccode, environment)
  File "/home/user/apps/web2py_folder/web2py/applications/my_app/controllers/appadmin.py", line 635, in <module>
  File "/home/user/apps/web2py_folder/web2py/gluon/globals.py", line 430, in <lambda>
    self._caller = lambda f: f()
  File "/home/user/apps/web2py_folder/web2py/applications/my_app/controllers/appadmin.py", line 325, in update
    if form.accepts(request.vars, session):
  File "/home/user/apps/web2py_folder/web2py/gluon/sqlhtml.py", line 1992, in accepts
    self.id_field_name]).update(**fields)
  File "/home/user/apps/web2py_folder/web2py/gluon/packages/dal/pydal/objects.py", line 2681, in update
    row = table._fields_and_values_for_update(update_fields)
  File "/home/user/apps/web2py_folder/web2py/gluon/packages/dal/pydal/objects.py", line 878, in _fields_and_values_for_update
    return self._compute_fields_for_operation(new_fields, to_compute)
  File "/home/user/apps/web2py_folder/web2py/gluon/packages/dal/pydal/objects.py", line 849, in _compute_fields_for_operation
    row.set_value(name, field.compute(row), field)
  File "/home/user/apps/web2py_folder/web2py/applications/my_app/models/db.py", line 497, in <lambda>
    db.news.image_file_oleodinamica_thumb.compute = lambda row: SMARTHUMB(row.image_file_oleodinamica, box)
  File "/home/user/apps/web2py_folder/web2py/applications/my_app/models/db.py", line 140, in SMARTHUMB
    img = Image.open(request.folder + 'uploads/' + image)
  File "/home/user/apps/web2py_folder/env/lib/python2.7/site-packages/PIL/Image.py", line 2766, in open
    fp = builtins.open(filename, "rb")
IOError: [Errno 2] No such file or directory: '/home/gael/apps/web2py_folder/web2py/applications/my_appuploads/news.image_file_oleodinamica.844c80079e794fbd.62726f777361626c655f636174616c6f6775652e6a7067.jpg'

Gaël Princivalle

unread,
Nov 1, 2020, 4:46:51 PM11/1/20
to web2py-users
I've found that in this function the upload folder was without a '/' at the beginning.
On Webfaction it was working fine, on Opalstack no.

I think it's due more to the Web2py version than to the hosting.
On Webfaction I've PIL (not Pillow) Python 2.7.5 with Web2py 2.14.6. and it works fine.
On Opalstack I've Pillow Python 2.7.5 with Web2py 2.20.4.
Anyway I've updated 'upload/' to '/upload/' and it works.
If it could be useful for someone else, as this SMARTHUMB function has been shared here.

def SMARTHUMB(image, box, fit=False, 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

        img = Image.open(request.folder + 'uploads/' + 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(request.folder +  'uploads/' + thumb)
        return thumb

Dave S

unread,
Nov 7, 2020, 5:35:43 PM11/7/20
to web2py-users
An interesting routine, but I just use PIL's thumbnail() method.  Could you explain the advantages of your routine?

/dps
 

Gaël Princivalle

unread,
Nov 8, 2020, 2:54:58 PM11/8/20
to web2py-users
> An interesting routine, but I just use PIL's thumbnail() method.  Could you explain the advantages of your routine?
I've no idea. I've followed an example years ago and I'm still working like that.

Can you post here and example of how do you use the PIL's thumbnail() method?

villas

unread,
Nov 9, 2020, 8:16:34 AM11/9/20
to web2py-users
Re: thumbnail.  Your code already uses it.
BTW  I think everyone uses Pillow now (drop in replacement for PIL with similar methods). 

Dave S

unread,
Nov 12, 2020, 4:40:25 AM11/12/20
to web2py-users


On Sunday, November 8, 2020 at 11:54:58 AM UTC-8, Gaël Princivalle wrote:
> An interesting routine, but I just use PIL's thumbnail() method.  Could you explain the advantages of your routine?
I've no idea. I've followed an example years ago and I'm still working like that.

Can you post here and example of how do you use the PIL's thumbnail() method?

Villas is right, you do use thumbnail().  I got lost in the other calculations, which seem to be for sizing calcullations.  I just used a fixed size because that's what my application likes.  I do this standalone at the moment, but I do plan to replace my current of exec of easy thumbnail sooner or later, and avoid process tossing.  Also, ET can't do webp or some GIFs.  Pillow can read webp, but can't write it.  (Pillow can also read many videos, but doesn't know which frame you want for a thumbnail.)

The standalone code looks like:

      img = Image.open(os.path.join(curpath, f))
      img
.thumbnail((200,200))
     
if f.endswith(".jpg"):
         img
.save(tf, "JPEG")
     
elif f.endswith(".gif"):
         img
.save(tf, "GIF")
     
elif f.endswith(".png"):
         img
.save(tf, "PNG")
     
elif tfa.endswith(".jpg"):
         
if img.mode != "RGB":
           
print img.mode
            img
=img.convert("RGB")
         img
.save(tfa, "JPEG")


/dps

Reply all
Reply to author
Forward
0 new messages