Build Your Own Image Compression App (With Python or JS)

Published by ImageCompressor | Updated 2025

Overview

Want to create your own image compression app? Whether you prefer Python or JavaScript, it's easier than ever to set up a tool that allows image uploads, compresses them, and returns a download link. This guide walks you through both Python and Node.js options.

Option 1: Python (Flask + Pillow)

Step 1: Install Requirements

pip install flask pillow

Step 2: Basic Flask App

from flask import Flask, request, send_file
from PIL import Image
import io

app = Flask(__name__)

@app.route('/compress', methods=['POST'])
def compress():
    file = request.files['image']
    img = Image.open(file)
    output = io.BytesIO()
    img.save(output, format='JPEG', optimize=True, quality=70)
    output.seek(0)
    return send_file(output, mimetype='image/jpeg')

Step 3: Frontend Form (HTML)

<form action="/compress" method="post" enctype="multipart/form-data">
  <input type="file" name="image">
  <button type="submit">Compress</button>
</form>

Option 2: JavaScript (Node.js + Sharp)

Step 1: Install Requirements

npm install express multer sharp

Step 2: Basic Express App

const express = require('express');
const multer = require('multer');
const sharp = require('sharp');
const fs = require('fs');

const app = express();
const upload = multer({ dest: 'uploads/' });

app.post('/compress', upload.single('image'), async (req, res) => {
  const inputPath = req.file.path;
  const outputPath = 'compressed-' + req.file.originalname;

  await sharp(inputPath)
    .jpeg({ quality: 70 })
    .toFile(outputPath);

  res.download(outputPath, () => {
    fs.unlinkSync(inputPath);
    fs.unlinkSync(outputPath);
  });
});

Step 3: Frontend Form (HTML)

<form action="/compress" method="post" enctype="multipart/form-data">
  <input type="file" name="image">
  <button type="submit">Compress</button>
</form>

Deployment Tips

Conclusion

With just a few lines of code, you can build a fully functional image compressor using either Python or JavaScript. Whether you’re creating a personal tool or launching a public app, this foundation gives you full control over image compression logic and interface.

← Back to All Articles