PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 6, 2022

[FIXED] How to manipulate save name of the images

 May 06, 2022     image, opencv, python, save     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing