如何操作图像的保存名称

How to manipulate save name of the images

我正在使用以下代码保存我的视频帧,但我也想将 save_path 添加到文件名中:

cv2.imwrite(f"{save_path}/{idx}.png", frame)

如何将 save_path 添加到文件名?

我需要它:

save_path/idxsavepath

由于我们缺少调试信息,这里是您应该编写的一段代码以避免路径问题。首先,检查目录是否找到,然后才继续。

import os
import cv2
import numpy as np


def main():
    # before continuing, must check if folder exists
    save_dir = r'D:\pics'
    valid_path = os.path.exists(save_dir)
    print('is {} a valid path ? {}'.format(save_dir, valid_path))

    if not valid_path:
        print('folder {} does not exist - create it and try again'.format(save_dir))
    else:
        idx = 0
        fake_frame = np.zeros(shape=(480, 640, 3))
        image_full_path = '{}/{}.png'.format(save_dir, idx)
        cv2.imwrite(image_full_path, fake_frame)
        print('image saved successfully on {}'.format(image_full_path))
    return


if __name__ == '__main__':
    main()