我们用python创建文件的时候需要按照一定的序号将文件放入规定的文件夹,创建文件夹的时候需要用到索引 i,j,k 等,以下是我写好的代码
import os
import numpy as np
from matplotlib import pyplot as plt
import time
path = 'G://phase8//original_npy1//'
# new_path = 'G://phase8//phs1//'
for root, dirs, files in os.walk(path):
for i in range(len(files)):
file_path = os.path.join(root, files[i])
phase = np.load(file_path)
for j in range(20):
new_path = 'G://phase8//phs1//'
if i < 10:
new_path = new_path + str('000') + str(i) + '_' + str(j)
if i >= 10 and i < 100:
new_path = new_path + str('00') + str(i) + '_' + str(j)
if i >= 100 and i < 1000:
new_path = new_path + str('0') + str(i) + '_' + str(j)
if i >= 1000:
new_path = new_path + str(i) + '_' + str(j)
os.makedirs(new_path)
phase1 = phase[256*j: 256*j+256,:] # 长横图
for k in range(40):
phase2 = phase1[:, k*128:256+k*128] # 256*256的图片 间隔为128
plt.imshow(phase2)
plt.axis('off')
plt.gcf().set_size_inches(256 / 100, 256 / 100)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top=1,bottom=0,right=1,left=0,hspace=0,wspace=0)
plt.margins(0, 0)
if i < 10:
# np.save(new_path + '//' + str('000') + str(i) + str('_') + str(j) + str('_') + str(k), phase2)
plt.savefig(new_path + '//' + str('000') + str(i) + str('_') + str(j) + str('_') + str(k), dpi=100)
if i >= 10 and i < 100:
# np.save(new_path + '//' + str('00') + str(i) + str('_')+str(j) + str('_') + str(k), phase2)
plt.savefig(new_path + '//' + str('00') + str(i) + str('_') + str(j) + str('_') + str(k), dpi=100)
if i >= 100 and i < 1000:
# np.save(new_path + '//' + str('0') + str(i) + str('_')+str(j) + str('_') + str(k), phase2)
plt.savefig(new_path + '//' + str('0') + str(i) + str('_') + str(j) + str('_') + str(k), dpi=100)
if i >= 1000:
# np.save(new_path + '//' + str(i) + str('_')+str(j) + str('_') + str(k), phase2)
plt.savefig(new_path + '//' + str(i) + str('_') + str(j) + str('_') + str(k), dpi=100)
print(i, j, k)
print('---save successfully---')