quality form parameter, and troubleshooting common errors when testing with Postman or curl.

- Create an upload route that accepts
multipart/form-dataPOSTs with animagefile. - Store the original upload in a configured upload folder.
- Use OpenCV to compress the image by changing JPEG quality (configurable via a
qualityform parameter). - Return JSON that points to the processed image path.
- Simple, production-proximate pattern: use an application factory to create the Flask app, configure an upload folder, and import route registrations inside the app context.
- Keep route logic concise and validate inputs early: verify file presence, filename, and the numeric
qualityrange (0–100). - Use OpenCV for image decoding/encoding and to control JPEG compression via
cv2.IMWRITE_JPEG_QUALITY.
Minimal routes (routes.py)
- This file assumes it’s imported after the Flask
apphas been created (for example, via an application factory that importsroutesinside an app context). The examples usecurrent_appas a reference to the Flask application; make sureroutesis imported while the application context is active. In many projects, using a Blueprint or importing theappinstance directly is preferred.
- The
/uploadhandler below:- Validates that the request contains an
imagefile part. - Verifies a filename is present.
- Ensures the configured upload folder exists and saves the original file.
- Reads the image using OpenCV, validates successful decode, and writes a compressed JPEG with the requested
quality(default is 10). - Returns a JSON response with the processed image path.
- Validates that the request contains an
- When using an application factory set the
UPLOAD_FOLDERon the app config. Importroutesinside the app context socurrent_appis available withinroutes.py. Avoid callingapp.run()inside modules that register routes.
- Be sure the form uses
enctype="multipart/form-data"and posts to/upload.
- Example curl command to test the
/uploadendpoint locally:
This Flask development server is not intended for production. Use a production WSGI server (for example,
gunicorn or uWSGI) behind a proper web server or load balancer for production deployments.- Ensure your upload folder exists and is writable by the Flask process, or create it programmatically with
os.makedirs(upload_folder, exist_ok=True). - Always validate and sanitize filenames in production. Use
werkzeug.utils.secure_filenameto avoid path traversal issues and remove unsafe characters. - If you prefer to return the processed image bytes directly instead of a filesystem path, use Flask’s
send_file()with the processed path or read the bytes and return them with the appropriateContent-Type. - To view detailed error pages during development, enable debug mode only for local development (for recent Flask versions set
FLASK_DEBUG=1or configure debug in your app’s config). - Consider storing images in a dedicated storage service (S3, GCS) for scalability rather than saving them locally on the web server for production deployments.
- If you need to handle large uploads or throttling, add size limits like
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024to prevent excessive memory or disk usage.