Error PIL path

1,496 views
Skip to first unread message

Márcio Luis Dresch

unread,
Jul 1, 2019, 2:20:10 AM7/1/19
to web2py-users
Hi, I'm new to web2py and am looking for a solution to a problem. The library is returning a path error while fetching an image for the thumbnail.
I took the example from:

http://www.web2pyslices.com/slice/show/1522/generate-a-thumbnail-that-fits-in-a-box,

which returns

File "C: \ Users \ marcio.INTRANET \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ PIL \ Image.py", line 2652, in open fp = builtins.open (filename, "rb")

FileNotFoundError: [Errno 2] No such file or directory: 'D: \\ web2py \\ applications \\ ImageUploads / article.picture.99b1bed4d9cf8a04.636172726f2e6a7067.jpg'
Can anyone help a clarify about the error?

windows10 64
Web2py 2.18.5-stable+timestamp.2019.04.08.04.22.03
(Rodando em Rocket 1.2.6, Python 3.7.2)

Nico de Groot

unread,
Jul 11, 2019, 4:28:17 AM7/11/19
to web2py-users
The example code assumes that the images are in the standard ‘uploads’ folder. Without looking at at your code, it seems that you defined - in your model file - the download folder to be ‘imageuploads’. That would explain why the thumbnail is not found.

Nico de Groot

Márcio Luis Dresch

unread,
Jul 19, 2019, 2:04:25 PM7/19/19
to web...@googlegroups.com
#this is a copy from http://www.web2pyslices.com/slice/show/1522/generate-a-thumbnail-that-fits-in-a-box

#modules\smarthumb.py
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
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)

img.thumbnail.show()

root, ext = os.path.splitext(image)
thumb = '%s_%s%s' % (root, name, ext)

#save it into a file-like object
img.save(request.folder + 'uploads/' + thumb)
 
return thumb

#models\db.py

Article = db.define_table('article',
Field("title"),
Field("article_text", "text"),
Field("picture", "upload", uploadfoder='uploads'),
Field("thumbnail", "upload", uploadfoder='uploads')
)


from smarthumb import SMARTHUMB

Article.thumbnail.compute = lambda row: SMARTHUMB(row.picture, (200,200))


#controllers\default.py
def addarticle():
form = SQLFORM(Article).process()
return dict(form=form)

def showarticle():
id = request.args(0) or redirect(URL('default', 'index'))
article = Article[id]
return dict(article=article)

#views\addarticle.html
<h1> Add an article </1>

{{=form}}

#views\showarticle.html
<article>
    <h1> {{=article.title}}</h1>
     
    <div style="width:200px; height:200px; max-width:200px; overflow:hidden;">
       <img src="{{=URL('default', 'download', args=article.image)}}" />
    </div>
     
    <p>{{=MARKMIN(article.article_text)}}</p>
     
    </article>

Em qui, 11 de jul de 2019 às 05:28, Nico de Groot <ndeg...@gmail.com> escreveu:
The example code assumes that the images are in the standard  ‘uploads’ folder. Without looking at at your code, it seems that you defined - in your model file - the download folder to be ‘imageuploads’. That would explain why the thumbnail is not found.

Nico de Groot   

--
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+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/20f37e0e-df44-4eba-a037-74ae476e21fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave S

unread,
Jul 20, 2019, 3:59:46 AM7/20/19
to web2py-users


On Friday, July 19, 2019 at 11:04:25 AM UTC-7, Márcio Luis Dresch wrote:


I'm not sure I agree with Nico, after reading your followup ... is "ImageUploads" the name of your application?

As copied here, "uploadfolder" has a typo ... check that your code doesn't actually have that.
And in your view, you use "article.image" to generate the URL ... but the model has "picture" and "thumbnail" but not "image".  Since that's in the slice article, it may have been a typo by Bruno, or I may be missing something.

Good luck!

/dps


To unsubscribe from this group and stop receiving emails from it, send an email to web...@googlegroups.com.

Rahul

unread,
Jul 23, 2019, 4:59:11 AM7/23/19
to web2py-users
Hi
  It looks like your escape sequence is missing \\ try using these before or after the folder path you are specifying. Again , just to add I used Pillow instead of PIL it works properly on windows.

Rahul
Reply all
Reply to author
Forward
0 new messages