[Python] How to get the same shading and colors with Lightsource in Python on two different...

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    I have two different images of maps that are located next to each other, and I want to shade them with the same method so that they fit seamlessly together with regards to shading and color. However, when I use LightSource in Python, they have different shadings despite using the same inputs. My code looks like this:

    import numpy as np
    import rasterio
    from matplotlib import cm
    from matplotlib.colors import ListedColormap, LinearSegmentedColormap, LightSource, rgb_to_hsv, hsv_to_rgb
    import matplotlib.pyplot as plt

    with rasterio.open("image_1.tiff", 'r') as ds:
    z1 = ds.read()

    with rasterio.open("image_2.tiff", 'r') as ds:
    z2 = ds.read()

    z1 = z1[0,:,:]
    z2 = z2[0,:,:]

    vmin = -12000
    vmax = 9000
    vexag = 0.05

    blend_mode = 'soft'

    cmap = cm.ocean.copy()
    ocean = cmap(np.linspace(0.30, 0.85, 144))

    colors = [
    (0.00, 'palegoldenrod'),
    (0.05, 'burlywood'),
    (0.30, 'sienna'),
    (0.50, 'tan'),
    (0.70, 'snow'),
    (1.00, 'white'),
    ]
    cmap = LinearSegmentedColormap.from_list('cm', colors)
    land = cmap(np.linspace(0, 1, 108))
    cmap = ListedColormap(np.concatenate((ocean, land)))

    light = LightSource(azdeg = 45, altdeg = 45)

    rgb1 = light.shade(
    z1, cmap = cmap,
    blend_mode = blend_mode, vert_exag = vexag, vmin = vmin, vmax = vmax
    )

    rgb1[:,:,-1][np.isnan(z1)] = 0
    rgb1 = np.uint8(rgb1*255)

    rgb2 = light.shade(
    z2, cmap = cmap,
    blend_mode = blend_mode, vert_exag = vexag, vmin = vmin, vmax = vmax
    )
    rgb2[:,:,-1][np.isnan(z2)] = 0
    rgb2 = np.uint8(rgb2*255)

    rgb3 = np.concatenate((rgb2, rgb1), axis=1)

    plt.figure()
    plt.imshow(rgb3)
    plt.show()


    The results look something like this:[​IMG]

    From what I gather, it has to do with the vertical exaggeration, vexag, which I have set to 0.05. If I set the exaggeration to 0, the results look like this: [​IMG]

    They now fit together the way I want, but you can no longer see any details in the image. Is it possible to keep the vertical exaggeration to 0.05 but still get the same colors and shading in the image?

    Continue reading...

Compartilhe esta Página