Images - Snailz Docs

Skip to content

# Images

Generate assay images.

## `images_generate(params, assays)`

Generate image files.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`params` | `AssayParams` | assay generation parameters | required
`assays` | `AllAssays` | generated assays | required

Returns:

Type | Description
--- | ---
`dict[str, Image]` | A dictionary of assay IDs and generated images.

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

    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    def images_generate(params: AssayParams, assays: AllAssays) -> dict[str, PilImage]:
        """Generate image files.

        Parameters:
            params: assay generation parameters
            assays: generated assays

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

## `_find_max_reading(assays, p_size)`

Find maximum assay reading value.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`assays` | `AllAssays` | generated assays | required
`p_size` | `int` | plate size | required

Returns:

Type | Description
--- | ---
`float` | Largest reading value across all assays.

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

    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    def _find_max_reading(assays: AllAssays, p_size: int) -> float:
        """Find maximum assay reading value.

        Parameters:
            assays: generated assays
            p_size: plate size

        Returns:
            Largest reading value across all assays.
        """
        result = 0.0
        for a in assays.items:
            for x in range(p_size):
                for y in range(p_size):
                    result = max(result, a.readings[x, y])
        return result

## `_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`

    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
    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.
        p_size = params.plate_size
        img_noise = params.image_noise
        img_size = (p_size * WELL_SIZE) + ((p_size + 1) * BORDER_WIDTH)
        img = Image.new("L", (img_size, img_size), color=BLACK)

        # Fill with pristine reading values.
        spacing = WELL_SIZE + BORDER_WIDTH
        draw = ImageDraw.Draw(img)
        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(WHITE * assay.readings[ix, iy] / scaling)
                draw.rectangle((x, y, x + WELL_SIZE, y + WELL_SIZE), color)

        # Add uniform noise (not provided by pillow).
        for x in range(img_size):
            for y in range(img_size):
                noise = random.randint(-img_noise, img_noise)
                old_val = img.getpixel((x, y))
                assert isinstance(old_val, int)  # for type checking
                val = max(BLACK, min(WHITE, old_val + noise))
                img.putpixel((x, y), val)

        # Blur.
        img = img.filter(ImageFilter.GaussianBlur(BLUR_RADIUS))

        return img