Skip to content

Images

Generate assay images.

make_image(params, assay, scaling)

Generate a single image.

Parameters:

Name Type Description Default
params

assay parameters

required
assay

assay to generate image for

required
scaling

color scaling factor

required

Returns:

Type Description

Image.

Source code in src/snailz/images.py
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
def make_image(params, assay, scaling):
    """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), 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(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 = _image_noise(params, array, img_size)

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

    # Apply blur filter
    img = img.filter(ImageFilter.GaussianBlur(BLUR_RADIUS))

    return img

_image_noise(params, array, img_size)

Add noise effects to numpy array before conversion to image.

Parameters:

Name Type Description Default
params

assay parameters

required
array

pristine numpy array

required
img_size

size of the image

required

Returns:

Type Description

Distorted numpy array.

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
def _image_noise(params, array, img_size):
    """Add noise effects to numpy array before conversion to image.

    Parameters:
        params: assay parameters
        array: pristine numpy array
        img_size: size of the image

    Returns:
        Distorted numpy array.
    """
    # Generate random noise array of the same shape
    noise = np.random.randint(
        -params.image_noise,
        params.image_noise + 1,
        size=(img_size, img_size),
        dtype=np.int16,
    )

    # Add noise to the original array
    noisy_array = np.clip(array.astype(np.int16) + noise, BLACK, WHITE).astype(np.uint8)

    return noisy_array