File size: 12,260 Bytes
fb77ca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7abeb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb77ca7
 
 
 
a7abeb8
fb77ca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""
Image Compressor Pro β€” Python Backend (FastAPI + Pillow)
========================================================
A high-performance, concurrent image compression API designed to replace
the Node.js + Sharp backend. Uses Pillow (with pillow-heif for HEIF/AVIF)
and FastAPI with StreamingResponse for efficient, non-blocking I/O.

Deployment target: HuggingFace Spaces (Docker) or local testing.
"""

import io
import logging
from contextlib import asynccontextmanager
from concurrent.futures import ThreadPoolExecutor

from fastapi import FastAPI, Request, Query, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse, PlainTextResponse
from starlette.concurrency import run_in_threadpool

from PIL import Image

# ---------------------------------------------------------------------------
# Optional: HEIF / AVIF support via pillow-heif
# ---------------------------------------------------------------------------
try:
    import pillow_heif
    pillow_heif.register_heif_opener()   # enables Image.open() for HEIF/AVIF
    HEIF_AVAILABLE = True
except ImportError:
    HEIF_AVAILABLE = False

# ---------------------------------------------------------------------------
# Logging
# ---------------------------------------------------------------------------
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s | %(levelname)-7s | %(message)s",
    datefmt="%H:%M:%S",
)
log = logging.getLogger("compressor")

# ---------------------------------------------------------------------------
# Thread pool β€” used to offload CPU-bound Pillow work
# ---------------------------------------------------------------------------
MAX_WORKERS = 4
_pool = ThreadPoolExecutor(max_workers=MAX_WORKERS)

# ---------------------------------------------------------------------------
# Lifespan (startup / shutdown)
# ---------------------------------------------------------------------------
@asynccontextmanager
async def lifespan(app: FastAPI):
    log.info("Compressor backend starting (workers=%d)", MAX_WORKERS)
    if HEIF_AVAILABLE:
        log.info("pillow-heif is available β€” HEIF/AVIF support enabled")
    else:
        log.warning("pillow-heif not installed β€” HEIF/AVIF output disabled")
    yield
    _pool.shutdown(wait=False)
    log.info("Compressor backend shut down")

# ---------------------------------------------------------------------------
# App
# ---------------------------------------------------------------------------
app = FastAPI(
    title="Image Compressor Pro API",
    version="1.0.0",
    lifespan=lifespan,
)

# CORS β€” allow all origins so the frontend can call from any domain
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
MAX_FILE_SIZE = 20 * 1024 * 1024  # 20 MB

# MIME types for each supported output format
MIME_MAP = {
    "jpeg": "image/jpeg",
    "png":  "image/png",
    "webp": "image/webp",
    "avif": "image/avif",
    "heif": "image/heic",
    "tiff": "image/tiff",
    "gif":  "image/gif",
    "bmp":  "image/bmp",
    "ico":  "image/x-icon",
    "jp2":  "image/jp2",
}

# ---------------------------------------------------------------------------
# Core compression logic (runs in thread pool)
# ---------------------------------------------------------------------------
def _compress_image(raw_bytes: bytes, quality: int, fmt: str) -> bytes:
    """
    Open *raw_bytes* as an image, compress it into *fmt* at the given
    *quality*, and return the resulting bytes.

    This is intentionally a **synchronous** function because Pillow is
    CPU-bound.  It is called via ``run_in_threadpool`` so the event loop
    is never blocked.
    """
    img = Image.open(io.BytesIO(raw_bytes))

    # Convert RGBA β†’ RGB for formats that don't support alpha
    if fmt in ("jpeg", "tiff", "bmp", "jp2") and img.mode in ("RGBA", "LA", "PA"):
        background = Image.new("RGB", img.size, (255, 255, 255))
        background.paste(img, mask=img.split()[-1])  # alpha composite
        img = background
    elif fmt in ("jpeg", "bmp", "jp2") and img.mode not in ("RGB", "L"):
        img = img.convert("RGB")
    elif fmt == "gif" and img.mode == "RGBA":
        # GIF supports palette transparency; convert RGBA β†’ P with transparency
        img = img.convert("RGBA")  # ensure consistent mode
        alpha = img.split()[-1]
        img = img.convert("RGB").convert("P", palette=Image.ADAPTIVE, colors=255)
        mask = Image.eval(alpha, lambda a: 255 if a <= 128 else 0)
        img.paste(255, mask)  # map transparent pixels to index 255
        # transparency index will be set during save

    out_buffer = io.BytesIO()

    if fmt == "jpeg":
        img.save(
            out_buffer,
            format="JPEG",
            quality=quality,
            optimize=True,
            progressive=True,
            subsampling="4:2:0" if quality < 90 else "4:4:4",
        )
    elif fmt == "png":
        # PNG quality mapped to compress_level (0-9, higher = smaller)
        compress_level = max(1, min(9, 9 - (quality // 12)))
        img.save(
            out_buffer,
            format="PNG",
            compress_level=compress_level,
            optimize=True,
        )
    elif fmt == "webp":
        img.save(
            out_buffer,
            format="WEBP",
            quality=quality,
            method=4,  # 0-6, higher = slower + better compression
        )
    elif fmt == "avif":
        # NOTE: Pillow 12.x has NATIVE AVIF support via AvifImagePlugin.
        # It does NOT use pillow-heif's 'chroma' parameter.
        # The correct parameter is 'subsampling' (default '4:2:0').
        # We MUST use '4:4:4' to prevent alpha edge bleeding on transparent images.
        
        if img.mode in ("RGBA", "LA", "PA") or "transparency" in img.info:
            img = img.convert("RGBA")
            import numpy as np
            data = np.array(img)
            alpha = data[:, :, 3]

            # Remove hidden RGB in fully transparent pixels
            data[:, :, :3][alpha == 0] = [0, 0, 0]

            img = Image.fromarray(data, "RGBA")
        elif img.mode not in ("RGB", "L"):
            img = img.convert("RGB")

        img.save(
            out_buffer,
            format="AVIF",
            quality=quality,
            subsampling="4:4:4",
        )
    elif fmt == "heif":
        if not HEIF_AVAILABLE:
            raise ValueError("HEIF output requires pillow-heif (not installed)")
        img.save(
            out_buffer,
            format="HEIF",
            quality=quality,
        )
    elif fmt == "tiff":
        img.save(
            out_buffer,
            format="TIFF",
            compression="tiff_lzw",  # lossless LZW compression
        )
    elif fmt == "gif":
        # Preserve animated GIF frames if present
        n_frames = getattr(img, 'n_frames', 1)
        if n_frames > 1:
            frames = []
            durations = []
            for i in range(n_frames):
                img.seek(i)
                frame = img.copy()
                if frame.mode != "P":
                    frame = frame.convert("RGBA").convert("P", palette=Image.ADAPTIVE, colors=256)
                frames.append(frame)
                durations.append(img.info.get('duration', 100))
            frames[0].save(
                out_buffer,
                format="GIF",
                save_all=True,
                append_images=frames[1:],
                duration=durations,
                loop=img.info.get('loop', 0),
                optimize=True,
            )
        else:
            if img.mode not in ("P", "L"):
                img = img.convert("P", palette=Image.ADAPTIVE, colors=256)
            img.save(
                out_buffer,
                format="GIF",
                optimize=True,
            )
    elif fmt == "bmp":
        if img.mode not in ("RGB", "L", "1"):
            img = img.convert("RGB")
        img.save(
            out_buffer,
            format="BMP",
        )
    elif fmt == "ico":
        # ICO spec limits dimensions to 256Γ—256
        max_ico = 256
        if img.width > max_ico or img.height > max_ico:
            img.thumbnail((max_ico, max_ico), Image.LANCZOS)
        if img.mode != "RGBA":
            img = img.convert("RGBA")
        img.save(
            out_buffer,
            format="ICO",
        )
    elif fmt == "jp2":
        if img.mode not in ("RGB", "L", "RGBA"):
            img = img.convert("RGB")
        img.save(
            out_buffer,
            format="JPEG2000",
            quality_mode="rates",
            quality_layers=[quality],
        )
    else:
        raise ValueError(f"Unsupported format: {fmt}")

    out_buffer.seek(0)
    return out_buffer.getvalue()

# ---------------------------------------------------------------------------
# Routes
# ---------------------------------------------------------------------------

@app.get("/ping", response_class=PlainTextResponse)
async def health_check():
    """Simple health-check endpoint compatible with the Node backend."""
    return "pong"


@app.get("/", response_class=PlainTextResponse)
async def root():
    """Root endpoint β€” useful for HuggingFace Spaces health probes."""
    return "Image Compressor Pro API is running"


@app.post("/api/process-stream")
async def process_stream(
    request: Request,
    quality: int = Query(default=80, ge=1, le=100),
    format: str = Query(default="jpeg", pattern="^(jpeg|png|webp|avif|heif|tiff|gif|bmp|ico|jp2)$"),
):
    """
    Receive a raw image in the request body (Content-Type: image/*),
    compress it using Pillow, and stream the result back.

    Query parameters
    ----------------
    quality : int (1–100)
        Compression quality. Higher = better quality, larger file.
    format : str
        Target output format (jpeg, png, webp, avif, heif, tiff).

    This mirrors the Node.js backend's ``POST /api/process-stream`` contract
    so the frontend works without changes.
    """
    # ---- Read the incoming image bytes (with size guard) ----
    body = await request.body()
    if len(body) == 0:
        raise HTTPException(status_code=400, detail="Empty request body")
    if len(body) > MAX_FILE_SIZE:
        raise HTTPException(
            status_code=413,
            detail=f"File too large ({len(body)} bytes). Max is {MAX_FILE_SIZE} bytes.",
        )

    # ---- Check HEIF/AVIF availability ----
    if format in ("avif", "heif") and not HEIF_AVAILABLE:
        raise HTTPException(
            status_code=400,
            detail=f"{format.upper()} format requires pillow-heif which is not installed.",
        )

    # ---- Offload CPU-bound compression to thread pool ----
    try:
        compressed = await run_in_threadpool(_compress_image, body, quality, format)
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        log.exception("Compression failed")
        raise HTTPException(status_code=500, detail=f"Compression error: {e}")

    # ---- Stream result back ----
    mime = MIME_MAP.get(format, "application/octet-stream")

    return StreamingResponse(
        io.BytesIO(compressed),
        media_type=mime,
        headers={
            "Content-Disposition": f"attachment; filename=compressed_image.{format}",
            "Content-Length": str(len(compressed)),
            "X-Original-Size": str(len(body)),
            "X-Compressed-Size": str(len(compressed)),
        },
    )


# ---------------------------------------------------------------------------
# Entry point for local development
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    import uvicorn

    uvicorn.run(
        "app:app",
        host="0.0.0.0",
        port=7860,
        workers=1,           # single worker for local dev; increase for prod
        log_level="info",
        reload=True,         # auto-reload on code changes during dev
    )