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)
        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 web2py+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/21b706a0-566c-4719-8ce1-0a8207a055bd%40googlegroups.com.
CODE LISTING
                upload_pic = form.vars.upload_photo 
                recordsID= form.vars.id
                
                print ("----------------")
        foto_path = os.path.join(request.folder + "static/user_uploads/" + 
upload_pic)
        #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

        

        ##Storing the absolute path for the image thumbnail in field. not 
relevant for us as we do not pic the file using that path. We are only setting 
the path here
        #Get platform
        import platform
        is_windows=(platform.system().lower().find("win") > -1)        
        is_linux=(platform.system().lower().find("lin") > -1)        
        is_mac=(platform.system().lower().find("mac") > -1)        

        if (is_windows == True):
            #print "is windows"
            thumbpath = os.path.join(request.folder, 'static', 'thumbs\\') + 
thumbnail_filename
        elif (is_linux == True):
            #print "is linux"
            thumbpath = os.path.join(request.folder, 'static', 'thumbs/') + 
thumbnail_filename
        elif (is_mac == True):
            #print "is macos"
            thumbpath = os.path.join(request.folder, 'static', 'thumbs/') + 
thumbnail_filename
        else: ## specify the default thumbnail
            #print "is macos"
            thumbpath = os.path.join(request.folder, 'static', 'thumbs/') + 
"default.thumbnail.jpg"

        print "thumbpath: ", thumbpath
     
        #print thumbpath
        #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:
                #print "infile: ", infile
                #print "outfile: ", outfile
                
                im = Image.open(infile)
                im.thumbnail(size)         
                #Check for image format
                image_format = (im.format)
                #print ("Image format: ", image_format)

                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
                
       
    elif form.errors:
        

Reply via email to