QGIS – Solving Rasterio Mask Washing Out Result in Python

maskingpythonqgisrasterio

Using the rio.mask.mask function to clip out a section of an ortho. The result .tif looks like it has been "washed" out. The pixel values from the ortho and the result are exactly the same, so seems like QGIS is reading it oddly? I set the symbology of both layers to exactly the same settings and still same issue. Process works on other orthos I have. Only difference is that this ortho has a projection of ESRI:102057 and my orthos that do work have a projection of EPSG:3857. Any additional information I can give?

def crop_ortho2(poly, ortho_dir, output_dir):
    with rio.open(ortho_dir) as src:
        shapefile = fiona.open(poly, 'r')
        shapes = [feature["geometry"] for feature in shapefile]
        out_image, out_transform = rio.mask.mask(src, shapes, crop=True)
        out_meta = src.meta
        print(out_meta)
        out_meta.update({"driver": "GTiff",
                         "height": out_image.shape[1],
                         "width": out_image.shape[2],
                         "transform": out_transform})
        output = output_dir + "0.tif"
        with rio.open(output, "w", **out_meta) as dest:
            dest.write(out_image)
            dest.close()     
        shapefile.close()
    src.close()

meta

{'driver': 'GTiff', 'dtype': 'uint8', 'nodata': 0.0, 'width': 16809, 'height': 23231, 'count': 4, 'crs': CRS.from_epsg(6339), 'transform': Affine(0.15, 0.0, 535020.9,
       0.0, -0.15, 4882392.3)}

Ortho
enter image description here

Result
enter image description here

Best Answer

This can be caused by the fourth band being interpreted as alpha / transparency. If that's the case, setting the ALPHA=unspecified geotiff creation option will tell the viewing software to ignore it instead.