Batch find the pictures in the folder according to TXT Python
編輯:Python
according to txt Batch find the pictures in the folder python
import re
from PIL import Image
import numpy as np
import os
data = []
path1 = r'G:\pachong\TEST_0526\2.txt' # txt File path
path_img1 = r'G:\pachong\TEST_0526\2' # Original image file path
path_img2 = r'G:\pachong\TEST_0526\222' # Save image new path
with open(path1, 'r') as fr:
data = fr.readlines()
# data = ''.join(data).strip('\n').splitlines()
data = ''.join(data).split('/')
data.sort(key=str, reverse=False)
data_new = "/".join(set(data)).split('/')
data = data_new
# print("".join(set(data)))
# ''.join() list To str
# s.strip(rm) Delete s At the beginning and end of rm character
# .splitlines() Returns a string to a list
# print(data)
for name in data:
# name1 = name.split('_')[1]
name1 = "TEST_0519_" + name + ".jpg"
path_old = os.path.join(path_img1, name1)
path_new = os.path.join(path_img2, name1)
im = Image.open(path_old)
im.save(path_new)
im.close()