RuntimeError:数据在[0.0,1.0]之外,未设置钳制

2024-05-16 03:15:06 发布

您现在位置:Python中文网/ 问答频道 /正文

def ContrastNBrightness1(img_array):
    img_cv2 = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
    img = copy.deepcopy(img_cv2)
    st.write("Contrast and Brightness Settings")
    orig = img.copy()
    contrast = st.slider('Contrast', 0.0, 10.0, 1.0)
    brightness = st.slider('brightness', 0.0, 500.0, 0.0)
    img = np.clip(img * contrast + brightness, 0, 255)
    img = imutils.resize(img, height=650)
    st.image(img)

当我尝试使用st.image(img)显示图像时,它给了我错误。如图所示,数据超出[0,1]的范围。所以我尝试用255img = np.divide(img, 255)除以img。它再次显示了相同的错误

如果我这样尝试:

def ContrastNBrightness1(img_array):
    img_cv2 = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
    img = copy.deepcopy(img_cv2)
    st.write("Contrast and Brightness Settings")
    orig = img.copy()
    contrast = st.slider('Contrast', 0.0, 10.0, 1.0)
    brightness = st.slider('brightness', 0.0, 500.0, 0.0)
    img = np.clip(img * contrast + brightness, 0, 255)
    img = imutils.resize(img, height=650)
    cv2.imwrite('tempImage.jpg', img)
    st.image('tempImage.jpg')

以上代码运行完美,能够显示图像。但在本例中,图像保存在本地存储器中,我想直接使用st.image(img)显示图像。如何处理


Tags: 图像imageimgdefnparraycv2slider