- Single-image optimization via a web UI
- Adjustable compression quality (1–100)
- A Flask REST API that performs image processing with OpenCV and validates output with Pillow
- A React frontend for file selection, quality control, upload flow, and results display
- Accepts a multipart/form-data POST with a
filefield and optionalqualityform field - Validates the uploaded bytes and image type
- Decodes the image into OpenCV, handles alpha / grayscale safely, and encodes to JPEG with the requested quality
- Verifies the resulting JPEG with Pillow to catch encoding issues
- Returns the compressed JPEG as binary data (image/jpeg)
quality, verifies with Pillow, and returns the image bytes.
Allowed image types (server-side validation)
A brief frontend overview
- The React app provides a file picker, a slider/input for quality (e.g., 77), and an “Optimize Image” button.
- On submit it sends a multipart/form-data POST to
/upload. - When the response arrives the frontend displays the optimized image along with statistics: original size, optimized size, and percent reduction (calculated client-side based on byte lengths).
- Use React hooks to manage file state, upload progress, and display results.
- Select a photo from your machine.
- Set the slider to the desired quality (e.g., 87).
- Click “Optimize Image”.
- The UI updates to show original size, optimized size, and the percentage reduction.

- Frontend collects the file +
qualityand posts to the Flask/uploadendpoint. - Flask reads the raw bytes, uses
imghdr.whatto validate the image type. - Bytes are converted to a NumPy buffer and decoded with OpenCV (
cv2.imdecode). - If the image includes alpha, convert BGRA -> BGR; handle grayscale safely.
- Re-encode to JPEG via
cv2.imencodewithcv2.IMWRITE_JPEG_QUALITYset to the requested value. - Wrap the encoded bytes in
BytesIOand verify that Pillow can open/verify the image (optional safety check). - Return the image to the client as
image/jpeg. The frontend measures byte sizes and computes percentage savings.
Tip: Keep
quality between 1 (most compression, lower quality) and 100 (least compression, near-original quality). The Flask code clamps out-of-range values to this range automatically.Warning: In production, enforce file-size limits and authentication, sanitize filenames, and consider scanning uploads for malicious content. Large images can consume significant memory while OpenCV decodes them.
- Full source code and step-by-step project files are available on GitHub: https://github.com/JeremyMorgan/Super-Image-Optimizer

- Flask — Python microframework
- OpenCV — Computer vision library
- Pillow — Python Imaging Library (PIL) fork
- React — Frontend library
- Project repository: https://github.com/JeremyMorgan/Super-Image-Optimizer