【问题标题】:Custom name for an opencv image (complex name)opencv 图像的自定义名称(复杂名称)
【发布时间】:2020-11-12 07:35:49
【问题描述】:

您好,我想从相机中取出一帧并将其保存在“图像”文件夹中,我希望图像具有名称+当前时间戳+.jpg,这样输入时出错:

cv2.imwrite(os.path.join(path2 , rn), img)

path2='images'rn=str(rightnow.strftime("%I:%M:%p")) 我收到此错误: could not find a writer for the specified extension in function 'cv::imwrite_'

请问我该怎么做,我搜索了但没有找到答案,而且我是 python 新手,在此先感谢

【问题讨论】:

    标签: python opencv operating-system


    【解决方案1】:

    接受的答案是os 特定的。

    如果在 Windows 中运行代码会发生什么?

    假设您有数百万个代码,您是否要将每个斜杠 / 更改为 \

    您应该使用os.path.sep 而不是静态斜杠声明。

    • 首先,去掉strftime中的:,你可以使用-或者_或者什么都不用。

      • rn = str(rightnow.strftime("%I%M%p"))
        
    • 其次,检查路径是否存在,如果不存在,则创建。

      • path3 = "".join([path2, os.path.sep, rn])
        
        if not os.path.exists(path3):
            os.mkdir(path3)
        
    • 三、创建镜像名称

      • save = "".join([path3, os.path.sep, "image_name.png"])
        
      • 如果你在循环内执行语句,你可以使用计数器

        • counter += 1
          save = "".join([path3, os.path.sep, "image_name{}.png".format(counter)])
          

    代码:


    import os
    import cv2
    from datetime import datetime
    
    path2 = 'img2'
    rightnow = datetime.now()
    rn = str(rightnow.strftime("%I%M%p"))
    img = cv2.imread("1.png")
    
    path3 = "".join([path2, os.path.sep, rn])
    
    if not os.path.exists(path3):
        os.mkdir(path3)
    
    save = "".join([path3, os.path.sep, "image_name.png"])
    
    cv2.imwrite(save, img)
    

    【讨论】:

      【解决方案2】:

      尝试改变:

      cv2.imwrite(os.path.join(path2 , rn), img)
      

      cv2.imwrite(os.path.join(path2 , rn)+".jpg", img)
      

      【讨论】:

        猜你喜欢
        • 2020-05-29
        • 2018-11-27
        • 1970-01-01
        • 2012-01-18
        • 1970-01-01
        • 1970-01-01
        • 2019-11-13
        • 2019-08-16
        • 2013-08-07
        相关资源
        最近更新 更多