程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

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()



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved