1 进行文件的拷贝,不只是图像文件
import os
import gdal
import numpy as np
import time
def Positive(nature_to_path,label_path,label_to_path,img_path,img_to_path,):
"""
进行负样本文件的拷贝
:param nature_to_path:负样本已经存在的的文件夹
:param label_path:全部的标签
:param label_to_path:要复制到的负样本标签文件夹
:param img_path:全部的影像
:param img_to_path:要复制到的负样本影像文件夹
:return:
"""
filenames = os.listdir(nature_to_path)
print(len(filenames))
for filename in filenames:
file_path = os.path.join(label_path, filename)
label_to = os.path.join(label_to_path, filename)
with open(file_path, "rb") as f:
res_one = f.read()
with open(label_to, "wb") as a:
a.write(res_one)
remote_path = os.path.join(img_path, filename)
remote_to = os.path.join(img_to_path, filename)
with open(remote_path, "rb") as f:
res_one = f.read()
with open(remote_to, "wb") as a:
a.write(res_one)
if __name__ == '__main__':
start = time.time()
Positive(
nature_to_path=r"I:\5GF2\5dataset\3NegativeData\3Negativenaturedata",
label_path=r"I:\5GF2\5dataset\1alldata\1labels",
label_to_path=r"I:\5GF2\5dataset\3NegativeData\1Negativelabels",
img_path=r"I:\5GF2\5dataset\1alldata\2rawdata",
img_to_path=r"I:\5GF2\5dataset\3NegativeData\2Negativerawdata"
)
end = time.time()
print("time_cost:",end-start)
2 拷贝含指定名字的图像
import os
path_png = r"I"
path_jpg = r"I:"
path_to_png = r"H:\01Hs"
path_to_jpg = r"H:\01HTut"
names = ["sc254305","sc254308","sc254309","lt497911","lt497908","lt521769"]
filenames = os.listdir(path_png)
for filename in filenames:
if filename[0:8] in names:
file_path = os.path.join(path_png, filename)
label_to = os.path.join(path_to_png, filename)
with open(file_path, "rb") as f:
res_one = f.read()
with open(label_to, "wb") as a:
a.write(res_one)
print("label ok")
filenamesjpg = os.listdir(path_jpg)
for filename in filenamesjpg:
if filename[0:8] in names:
file_path = os.path.join(path_jpg, filename)
label_to = os.path.join(path_to_jpg, filename)
with open(file_path, "rb") as f:
res_one = f.read()
with open(label_to, "wb") as a:
a.write(res_one)
print("images ok")
|