Save a upload Image with diferent size using flask

Viewed 33

I created a function to upload an image from web browser via flask. I need to save the image with reduced size. I made this. But my approach is not so much smart. I need to uppload the image, save image, open image, and resize the image, and then save the final image. And I would like to uppload image, resize and save final image. Here is my code:

def upload_image():
    if 'file' not in request.files:
        flash('No file part')
        #return redirect(request.url)
    file = request.files['file']
    if file.filename == '':
        flash('No image selected for uploading')
        #return redirect(request.url)

    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(os.path.join('foto_01.jpeg'))

        #### Padronizando tamanho de foto
        basewidth = 600
        # My image is a 200x374 jpeg that is 102kb large
        foo = Image.open('foto_01.jpeg')
        wpercent = (basewidth/float(foo.size[0]))
        # I downsize the image with an ANTIALIAS filter (gives the highest quality)
        hsize = int((float(foo.size[1])*float(wpercent)))
        foo = foo.resize((basewidth,hsize), Image.ANTIALIAS)
        foo = ImageOps.exif_transpose(foo)
        foo.save("foto_02.jpeg",optimize=True,quality=95)

I tried this at:

wpercent = (basewidth/float(filename.size[0])) # Use filename direct here.

but I got this error for that:

AttributeError: 'str' object has no attribute 'size'
0 Answers
Related