Hi Dave and Everyone,
I followed your advice -- Thanks for the code cleanup - However
the issues still exist
*ISSUE*#1 - The photo is still being saved in
[E:\web2py_src\web2py\applications\*artpicstatic*\user_uploads]
Correct path where it should be saved is [infile:
E:\web2py_src\web2py\applications\artpic\static\user_uploads\]
I simply do not understand why the application is saving the file in above
path and not in the correct path in green. We have removed the problem
causing code [foto_path = (request.folder + "static/user_uploads/" +
upload_pic) ] from this method so where is it pulling it from and saving
the files to this location only. Where is this magic happening from? Is it
web2py
*ISSUE*#2 - As the application cannot find the file in the correct path
mentioned above it fails to create a thumbnail for it
UPDATED CODE listing
form = SOLIDFORM(db.fotoz , fields=fields, submit_button='Upload Foto')
#if form.accepts(request.vars, session):
if form.process().accepted:
upload_pic = form.vars.upload_photo
recordsID= form.vars.id
#Thumbnail code
print ("----------------")
infile = os.path.join(request.folder, "static", "user_uploads",
upload_pic)
print "infile: " , infile
thumbnail_filename = "%s.thumbnail.jpg" % recordsID
print "thumbnail_filename: ", thumbnail_filename
thumbpath = outfile = os.path.join(request.folder, "static",
"thumbs" , thumbnail_filename)
print "thumbpath and outfile: ", outfile
#Insert thumbnail and thumbnail path in db imgthumb field.
db(db.fotoz.id==recordsID).update(imgthumb=thumbnail_filename,
imgthumbpath=thumbpath) ##outfile
if infile != outfile:
try:
im = Image.open(infile)
im.thumbnail(size)
#Check for image format
image_format = (im.format)
#print ("Image format: ", image_format)
### ================ Watermark stuff ======================
## Watermark Text --
width, height = im.size
draw = ImageDraw.Draw(im)
watermark_text = "WATER"
watermark_font = ImageFont.truetype('arial.ttf', 20)
textwidth, textheight = draw.textsize(watermark_text,
watermark_font)
# calculate the x,y coordinates of the text
margin = 5
x = width - textwidth - margin
y = height - textheight - margin
# draw watermark in the bottom right corner
## Specify this to fill font with red color ,
fill=(128,0,0,128)) OR #, fill=shadowcolor) OR fill="red" or
fill="#ff0000" -- work
draw.text((x, y), watermark_text, font=watermark_font,
opacity=0.25)
## --- Watermark text ends ---
#Process various image formats for making thumbnails.
Currently supporting JPG/JPEG, GIF and PNG
if (image_format == "JPEG"):
im.save(outfile, "JPEG")
if (image_format == "JPG"):
im.save(outfile, "JPG")
if (image_format == "GIF"):
im.save(outfile, "GIF")
if (image_format == "PNG"):
im.save(outfile, "PNG")
except IOError:
print "Cannot create thumbnail (un-recognized format) for: "
, infile
#pass
response.flash='Photo uploaded'
*OUTPUT *- Path output for above code --* NOTE The infile must be saved in
the path mentioned below but it does not exist there. *
----------------
infile:
E:\web2py_src\web2py\applications\artpic\static\user_uploads\fotoz.upload_photo.bf976468faa81f30.3030372e4a5047.JPG
thumbnail_filename: 99.thumbnail.jpg
thumbpath and outfile:
E:\web2py_src\web2py\applications\artpic\static\thumbs\99.thumbnail.jpg
Cannot create thumbnail (un-recognized format) for:
E:\web2py_src\web2py\applications\artpic\static\user_uploads\fotoz.upload_photo.bf976468faa81f30.3030372e4a5047.JPG
Regards,
Rahul
On Friday, August 30, 2019 at 1:42:44 AM UTC+5:30, Dave S wrote:
>
>
>
> On Thursday, August 29, 2019 at 12:21:41 PM UTC-7, Rahul wrote:
>>
>> Hi Val and Dave,
>>
>> Have a look at the code below - the line foto_path=... below works but it
>> uploads the photos to "web2py\applications\artpicstatic\user_uploads"
>> folder instead of web2py\applications\artpic\static\user_uploads folder. It
>> also generates the thumbnail file. Now if I use *foto_path =
>> os.path.join(request.folder + "static/user_uploads/" + upload_pic)* it
>> shows the right path after normalization but does not upload the photo at
>> all. And since it does not upload the photo the rest of the code fails to
>> generate thumbnails etc and throws this error . m using windows 10 with
>> python 2.7.13
>>
>> - <type 'exceptions.IOError'> [Errno 2] No such file or directory:
>> 'E:\\web2py_src\\web2py\\applications\\artpic\\static\\user_uploads\\fotoz.upload_photo.82bfeee5a1affabd.3033372e4a5047.JPG'
>>
>> CODE:
>> -------
>> #foto_path = (request.folder + "static/user_uploads/" +
>> upload_pic)
>> print ("----------------")
>> foto_path = (request.folder + "static/user_uploads/" + upload_pic)
>>
>
> Bzzt. Concantenating different parts of the path is tetchy. I'd use
> os.path.join() ALWAYS, and let it provide the separators. You missed one,
> it seems.
>
> /dps
>
>
> print "Foto Path: ", foto_path
>>
>> normal_foto_path = os.path.normpath(foto_path)
>> print "Normal Foto Path: ", normal_foto_path
>>
>> infile = normal_foto_path
>> print "infile: " , infile
>>
>> thumbnail_filename = ("%s" % recordsID + ".thumbnail.jpg")
>> print "thumbnail_filename :", thumbnail_filename
>>
>> outfile_path = os.path.join(request.folder, "static/thumbs/" +
>> thumbnail_filename)
>> print "outfile_path: ", outfile_path
>>
>> normal_outfile_path = os.path.normpath(outfile_path)
>> print "normal_outfile_path ", normal_outfile_path
>>
>> outfile = normal_outfile_path
>> print "outfile: ", outfile
>> -----------
>> OUTPUT ITERATIONS:
>>
>> [1]
>>
>> Foto Path:
>> E:\web2py_src\web2py\applications\artpicstatic/user_uploads/fotoz.upload_photo.8c9837615e4fa932.3035382e4a5047.JPG
>> Normal Foto Path:
>> E:\web2py_src\web2py\applications\artpicstatic\user_uploads\fotoz.upload_photo.8c9837615e4fa932.3035382e4a5047.JPG
>> infile:
>> E:\web2py_src\web2py\applications\artpicstatic\user_uploads\fotoz.upload_photo.8c9837615e4fa932.3035382e4a5047.JPG
>> thumbnail_filename : 64.thumbnail.jpg
>> outfile:
>> E:\web2py_src\web2py\applications\artpic\static\thumbs\64.thumbnail.jpg
>> ----------------
>> [2]
>>
>> Foto Path:
>> E:\web2py_src\web2py\applications\artpicstatic/user_uploads/fotoz.upload_photo.838dbfb13f66470d.3030312e4a5047.JPG
>> Normal Foto Path:
>> E:\web2py_src\web2py\applications\artpicstatic\user_uploads\fotoz.upload_photo.838dbfb13f66470d.3030312e4a5047.JPG
>> infile:
>> E:\web2py_src\web2py\applications\artpicstatic\user_uploads\fotoz.upload_photo.838dbfb13f66470d.3030312e4a5047.JPG
>> thumbnail_filename : 65.thumbnail.jpg
>> outfile_path:
>> E:\web2py_src\web2py\applications\artpic\static/thumbs/65.thumbnail.jpg
>> normal_outfile_path
>> E:\web2py_src\web2py\applications\artpic\static\thumbs\65.thumbnail.jpg
>> outfile:
>> E:\web2py_src\web2py\applications\artpic\static\thumbs\65.thumbnail.jpg
>>
>> ------------
>> [3]
>>
>> infile:
>> E:\web2py_src\web2py\applications\artpicstatic\user_uploads\fotoz.upload_photo.862f9808c7622ca7.3033372e4a5047.JPG
>> thumbnail_filename: 79.thumbnail.jpg
>> outfile:
>> E:\web2py_src\web2py\applications\artpic\static\thumbs\79.thumbnail.jpg
>> thumbpath:
>> E:\web2py_src\web2py\applications\artpic\static\thumbs\79.thumbnail.jpg
>> Cannot create thumbnail (un-recognized format) for:
>> E:\web2py_src\web2py\applications\artpicstatic\user_uploads\fotoz.upload_photo.862f9808c7622ca7.3033372e4a5047.JPG
>>
>>
>> SCREENSHOT
>>
>>
>>
>>
>> CODELISTING IS ATTACHED
>>
>> Looks like a simple answer but feels like my code is badly written...
>>
>> Regards,
>>
>> Rahul
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> On Thursday, August 29, 2019 at 2:17:49 AM UTC+5:30, Val K wrote:
>>>
>>> It is no need to use different code for win/linux, since it is Python!
>>> There is os.path.normpath(your_path) that does all stuff with slashes, I
>>> have app that works on both platforms just fine (without platform checking
>>> at all)
>>> My way - always use '/' in path, and at the end of all manipulations
>>> performs normalisation: path = os.path.normpath(path)
>>> And keep in mind that os.path.join() also works with filenames, so you
>>> can just os.path.join('foo', 'bar', 'baz.thumbnail.jpg')
>>>
>>>
>>>
>>> On Wednesday, August 28, 2019 at 9:05:11 PM UTC+3, Rahul wrote:
>>>>
>>>> Hi Dave,
>>>>
>>>> upload_pic is the form variable that user specifies for uploading the
>>>> file from local disk. Basically it is the filename. I removed the braces
>>>> for
>>>> outfile = (thumbpath)
>>>> but this is still not working. Any more suggestions ?
>>>>
>>>>
>>>> Regards
>>>> Rahul
>>>>
>>>> On Tuesday, August 27, 2019 at 2:21:45 PM UTC+5:30, Rahul wrote:
>>>>>
>>>>> Hey Dave,
>>>>> I was facing issues with path storage in a db field so I
>>>>> started using this method. I dont need outfiles curly braces will revisit
>>>>> the code today if possible and get back with the details. This is my
>>>>> modified code, previous one was straight forward and streamlined code and
>>>>> was using simple code.Thanks for the questions I’ll try getting in
>>>>> details.
>>>>>
>>>>> Regards,
>>>>>
>>>>> Rahul
>>>>
>>>>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/web2py/660234c3-f179-4683-9297-c91c041109d7%40googlegroups.com.