根據txt批量找出文件夾裡面的圖片python
import re
from PIL import Image
import numpy as np
import os
data = []
path1 = r'G:\pachong\TEST_0526\2.txt' # txt文件路徑
path_img1 = r'G:\pachong\TEST_0526\2' # 原圖像文件路徑
path_img2 = r'G:\pachong\TEST_0526\222' # 保存圖像新路徑
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轉為str
# s.strip(rm) 刪除s中開頭結尾處的rm字符
# .splitlines() 將字符串返回列表
# 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()