'Reclassifying image creates unwanted artifacts

I have a lot of (Sentinel-2) raster cloudcover-images, which I would like to reclassify. It should assign each cell either a 1 or a 0, depending if the value is higher than var or not. It mostly works, except for a few (seemingly random) cells that get the value 2.

My code:

import rasterio as rio

def Reclassify(path_in, path_out):
    
    # path_in = full path to image which should be reclassified
    # path_out = output location for the reclassified file
    var = 40  # Value that decides the border between cloud/no cloud

    with rio.open(path_in) as src:
        
        array = src.read()
        profile = src.profile

        # Reclassify
        array[np.where(array < var)] = 0  # No clouds
        array[np.where(array >= var)] = 1  # Clouds
        
    with rio.open(path_out, 'w', **profile) as dst:
        # Save changes
        dst.write(array)

The 2's are seemingly random, but they do appear at the exact same places everytime I run the code.

Result loaded in QGIS below: Result in QGIS



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source