File size: 1,046 Bytes
1c77735
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
from PIL import Image
from app.preprocessing.base import PreprocessingStep, PreprocessingContext, PreprocessingError


class DecodeStep(PreprocessingStep):
    name = "decode"
    description = "Decode raw bytes into a PIL Image object"
    version = "1.0.0"
    order = 2
    enabled = True
    required = True

    async def process(self, ctx: PreprocessingContext, params: dict) -> PreprocessingContext:
        try:
            image = Image.open(io.BytesIO(ctx.raw_bytes))
            image.load()
        except Exception as e:
            raise PreprocessingError(f"Failed to decode image: {e}")

        ctx.image = image
        ctx.metadata["original_width"] = image.width
        ctx.metadata["original_height"] = image.height
        ctx.metadata["original_mode"] = image.mode

        channels = len(image.getbands())
        ctx.step_outputs["decode"] = {
            "width": image.width,
            "height": image.height,
            "mode": image.mode,
            "channels": channels,
        }

        return ctx