Python Extracts all of the. jpg files in the transfer folder and looks at each frame

  • 2021-07-03 00:31:12
  • OfStack

In python, you can replace\ in the path with/to avoid escaping.

The os. walk method can extract root, dirs and files of files under the target path. Operate on each file later.

The slicing operation [:] determines whether it is an. jpg or. JPG file.

The copy method of shutil copies files from the old path to the new path.

glob's glob method extracts all the pictures of the target folder, and displays and saves each picture.

Detailed codes and comments are as follows:


import os
import shutil
import glob
import cv2
 
path = 'C:/Users/deepw/Desktop/testfile'
new_path = 'D:/new'
for root,dirs,files in os.walk(path): # Extract all under the folder jpg File copy to a new folder 
  for i in range(len(files)):
    if files[i][-3:] == 'jpg' or files[i][-3:] == 'JPG':
      file_path = root + '/' + files[i]
      new_file_path = new_path + '/' + files[i]
      shutil.copy(file_path,new_file_path)
 
 
 
img_path = glob.glob('D:/new/*.jpg') # Get all pictures in the new folder 
i=1
for each in img_path:
  img = cv2.imread(each, cv2.IMREAD_UNCHANGED)
  cv2.imshow('Image', img) # Sequential display of each 1 Frame 
  k=cv2.waitKey(0) # Every 1 Frame latency is infinity 
  if k == ord('s'): # When pressed s Save this frame when pressing the key, and skip to the next key without saving 1 Frame 
    cv2.imwrite('D:/want/%d.jpg'%i,img,[int( cv2.IMWRITE_JPEG_QUALITY), 100])
    cv2.destroyAllWindows()
  else:
    cv2.destroyAllWindows()
  i=i+1

Related articles: