RuntimeError: Data is outside [0.0, 1.0] and clamp is not set

696 views Asked by At
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)

when i try to show the image using st.image(img) it is giving me the error. as it shows data is outside the range of [0,1]. so i try to divide the img with 255 img = np.divide(img, 255) . Again it shows the same error.

if i try like this:

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')

Above code runs perfectly and able to show the image. But in this case image is saved in the local storage and i want directly to show image using st.image(img). How to handle this?

1

There are 1 answers

3
MonsijB On

Use sk.image(...,clamp=True). For more information refer to : https://discuss.streamlit.io/t/runtimeerror-data-is-outside-0-0-1-0-and-clamp-is-not-set/12910