【问题标题】:cv2 frame write file taking too much sizecv2 帧写入文件占用太大
【发布时间】:2021-02-18 18:14:57
【问题描述】:

截图:

基本上,我正在尝试创建一个包含视频所有帧的文件夹,并稍后在代码中对其进行编辑,但是,我遇到了保存时 img/frame 的文件大小过高的问题我一直在尝试调查,但到目前为止我没有运气。

我们将不胜感激!


VideoCap = cv2.VideoCapture("./Path/Path.mp4");

FolderPath = "./Path/Path";

if not os.path.exists(FolderPath):
    print(f"{FolderPath} doesn't exists. Creating...");
    os.makedirs(FolderPath);

    Success, Frame = VideoCap.read();
    Count = 0;

    while Success:
        cv2.imwrite(FolderPath+"/Frame%d.png" % Count, Frame); # The size is way too much! Literally 1/15th of the video just in a frame.
        print("Frame %d has been written" % Count);
        Count+=1;
        Success, Frame = VideoCap.read();
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
else:
    print(f"{FolderPath} already exists. Moving on...");

【问题讨论】:

    标签: python opencv cv2 opencv-python


    【解决方案1】:

    在写入图像时使用压缩参数。参考documentation

    cv2.imwrite(filename, img,  [cv2.IMWRITE_PNG_COMPRESSION, compression_value])
    #compression_value range 0-9 
    

    对于 PNG,它可以是从 0 到 9 的压缩级别(CV_IMWRITE_PNG_COMPRESSION)。较高的值意味着较小的尺寸和较长的压缩时间。默认值为 3。

    【讨论】:

    • 感谢您的帮助;虽然我确实使用了它,但它并没有减少很多图像大小,最多可能在 200kb 左右。我找到了一种更简单的方法,我只是将 png(文件扩展名)转换为 jpg,并使用 cv2 调整它的大小!
    • Png 用于无损压缩,jpg 用于有损压缩。所以jpg可以压缩到更大的程度。
    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 2019-08-23
    • 2021-07-17
    • 2017-12-16
    • 2018-06-23
    • 1970-01-01
    相关资源
    最近更新 更多