Images - Snailz Docs

Skip to content

# Images

Generate assay images.

## `AllImages`

Bases: `BaseModel`

A set of generated images.

Source code in `src/snailz/images.py`

    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    class AllImages(BaseModel):
        """A set of generated images."""

        model_config = {"arbitrary_types_allowed": True, "extra": "forbid"}

        @staticmethod
        def generate(params: AssayParams, assays: AllAssays) -> dict:
            """Generate image files.

            Parameters:
                params: assay generation parameters
                assays: assays to generate images for

            Returns:
                A dictionary of assay IDs and generated images.
            """
            scaling = float(math.ceil(assays.max_reading() + 1))
            return {a.ident: _make_image(params, a, scaling) for a in assays.items}

### `generate(params, assays)` `staticmethod`

Generate image files.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`params` | `AssayParams` | assay generation parameters | required
`assays` | `AllAssays` | assays to generate images for | required

Returns:

Type | Description
--- | ---
`dict` | A dictionary of assay IDs and generated images.

Source code in `src/snailz/images.py`

    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    @staticmethod
    def generate(params: AssayParams, assays: AllAssays) -> dict:
        """Generate image files.

        Parameters:
            params: assay generation parameters
            assays: assays to generate images for

        Returns:
            A dictionary of assay IDs and generated images.
        """
        scaling = float(math.ceil(assays.max_reading() + 1))
        return {a.ident: _make_image(params, a, scaling) for a in assays.items}

## `_make_image(params, assay, scaling)`

Generate a single image.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`params` | `AssayParams` | assay parameters | required
`assay` | `Assay` | assay to generate image for | required
`scaling` | `float` | color scaling factor | required

Returns:

Type | Description
--- | ---
`Image` | Image.

Source code in `src/snailz/images.py`

    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
    def _make_image(params: AssayParams, assay: Assay, scaling: float) -> PilImage:
        """Generate a single image.

        Parameters:
            params: assay parameters
            assay: assay to generate image for
            scaling: color scaling factor

        Returns:
           Image.
        """
        # Create blank image array.
        p_size = params.plate_size
        img_size = (p_size * WELL_SIZE) + ((p_size + 1) * BORDER_WIDTH)
        array = np.full((img_size, img_size), utils.BLACK, dtype=np.uint8)

        # Fill with pristine reading values.
        spacing = WELL_SIZE + BORDER_WIDTH
        for ix, x in enumerate(range(BORDER_WIDTH, img_size, spacing)):
            for iy, y in enumerate(range(BORDER_WIDTH, img_size, spacing)):
                color = math.floor(utils.WHITE * assay.readings[ix, iy] / scaling)
                array[y : y + WELL_SIZE + 1, x : x + WELL_SIZE + 1] = color

        # Add noise to numpy array before converting to image
        array = model.image_noise(params, array, img_size)

        # Convert to PIL Image
        img = Image.fromarray(array)

        # Apply blur filter
        return model.image_blur(img)