Okay, so I’m trying to make an image resizer with flask and Pillow and I am done with the 1st two parts:
- Show user uploaded images with Dropzone
- Save user uploaded image as it is for now into a static folder
Now, I want to resize these images before saving them. For resizing them I have a form that gets width and height from the user.
Html code –
<form>
<p>IN PIXELS</p>
<label for="width">Width - </label>
<input type="number" id="width" name="width" value="width">
<label for="height">Height - </label>
<input type="number" id="height" name="height" value="width">
<input type="submit" id="submit">
</form>
Script that gets user image –
@app.route('/', methods=['GET', 'POST'])
def retrieve_images():
if request.method == 'POST':
file = request.files['file']
filename = secure_filename(file.filename)
if filename != '':
file_ext = os.path.splitext(filename)[1]
if file_ext not in app.config['UPLOAD_EXTENSIONS'] or
file_ext != validate_image(file.stream):
return 'Invalid Image', 404
img_file = Image.open(file)
width = request.form.get("width")
height = request.form.get("height")
res_img = img_file.resize((width, height), Image.LANCZOS)
return res_img.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return render_template('index.html')
And, I have this type error –
return self._new(self.im.resize(size, resample, box))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: ‘NoneType’ object cannot be interpreted as an integer
I tried to search online but could not find any relevant answer to this.
user25346712 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.