Creating a file in a non-existing folder using OpenCV in Python

前端 未结 1 1759
悲&欢浪女
悲&欢浪女 2021-02-14 04:39

i am trying to create an image file using opencv in python. when i am creating it in same folder file is created

          face_file_name = \"te.jpg\"
                   


        
相关标签:
1条回答
  • 2021-02-14 05:19

    cv2.imwrite() will not write an image in another directory if the directory does not exist. You first need to create the directory before attempting to write to it:

    import os
    dirname = 'test'
    os.mkdir(dirname)
    

    From here, you can either write to the directory without changing your working directory:

    cv2.imwrite(os.path.join(dirname, face_file_name), image)
    

    Or change your working directory and omit the directory prefix, depending on your needs:

    os.chdir(dirname)
    cv2.imwrite(face_file_name, image)
    
    0 讨论(0)
提交回复
热议问题