swordfish
swordfish

Reputation: 959

How to resize images before uploading them in Flask?

I have four images i want to upload them, but their sizes are very big it takes long time to upload them into the site.

I want to resize each one of them, i wrote a small route that handle the request:

@team_route.route('/team/dashboard/add/product', methods=['GET', 'POST'])
@requires_auth
@master_login_required(role='master')
def team_add_product():
    form = AddProduct()
    imagesList = []
    size = 1024, 764
    if request.method == 'POST' and form.is_submitted():
        product = Goods()
        file = request.files.getlist("image[]")
        if file:
            for zipfile in file:
                fi = Image.open(BytesIO(zipfile.stream.read()))
                im.thumbnail(size)
                img2 = im.rotate(-90, expand=True)
                img2.seek(0)
                img2.save(UPLOAD_FOLDER + '/crops/' + zipfile)

When i hit upload it gives me this error:

AttributeError: 'JpegImageFile' object has no attribute 'read'

Please any help solving this ?

Upvotes: 2

Views: 2504

Answers (1)

gtzinos
gtzinos

Reputation: 1197

You have to remove .stream

zipfile.read() its the only you need.

Upvotes: 1

Related Questions