Friday, May 6, 2022

[FIXED] How to manipulate save name of the images

Issue

I am saving my video frames with below code but I want to add save_path to file name as well:

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

How could I add save_path to file name?

I need it as:

save_path/idxsavepath


Solution

since we lack the debugging info, here is a piece of code you should have written to avoid path problems. first, check that the directory is found and only then continue.

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()


Answered By - gilad eini
Answer Checked By - Cary Denson (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.