Search code examples
pythondata-scienceopencvimread

How to save cv2.imwrite into a variable and called it in cv.imread


I am using cv2.imwrite to save my new edited image. Now I want to call that in cv.imread. I want to write my new image to variable so I can call that variable in cv.imread but it looks like cv.imread dont read the variable.

Currently I am doing as follows

cv2.imwrite("C:/Users/Imp/MyKey16.png", EditImg)
img1 = cv.imread('C:/Users/Imp/MyKey16.png',0)

Instead of it I want to go for;

EditedImage = cv2.imwrite("C:/Users/Imp/MyKey16.png", EditImg)
img1 = cv.imread(EditedImage)

How to achieve it?


Solution

  • That's not possible, because imread takes a string as argument (filename) while iwrite second argument is a cv::Mat. Why don't you store the filename in a variable?

    filename = 'C:/Users/Imp/MyKey16.png'
    EditedImage = cv2.imwrite(filename, EditImg)
    img1 = cv.imread(filename)
    

    But again, unless you have some valid reason, there should be no need to save the image locally, just copy the cv::Mat as suggested in the answer of Muhammed Yücel.