Use Python to filter the file script method

  • 2021-01-06 00:39:23
  • OfStack

When I was working on a project, I encountered the need to mark several picture data in the data set. As a programmer, I wrote this Python script implementation in order to avoid manually filtering one picture one picture.

The Python script is as follows:


# from PIL import Image
import csv
import os
import shutil
 
filename = 'img.txt'
 
def readImageName():
 with open(filename) as f:
  lines = f.readlines()
  imgnames = []
  for line in lines:
   imgnames.append(line.strip().strip(".jpg")[-4:])
  print(imgnames)
  return imgnames
 
def pickImg():
 pickImageNames = readImageName()
 #  Iterate through the file names of all photo sets 
 for image in os.listdir(r"C:\Users\Administrator.PC-201708272051\Desktop\ The project team \text_detect_label_data\China_SameBrowser"):
  # print(image[:-4])
  if image[:-4] in pickImageNames:
   # pickImage = Image.open((r"C:\Users\Administrator.PC-201708272051\Desktop\ The project team \text_detect_label_data\China_SameBrowser/%s") % image)
   # pickImage.save((r"C:/Users/Administrator.PC-201708272051/Desktop/labeldata/%s") % image)
 
   oldname= r"C:\Users\Administrator.PC-201708272051\Desktop\ The project team \text_detect_label_data\China_SameBrowser/" + image
   newname= r"C:/Users/Administrator.PC-201708272051/Desktop/labeldata/" + image
   shutil.copyfile(oldname,newname)
 
# readImageName()
pickImg()
 

Related articles: