求精简代码
基础不扎实,在写入文件等地方总是不用循环就写不出来。比如想要把列表中的每个元素分行写入之类的,所以代码就一个一个循环乱七八糟的,大佬们能不能教一下如何精简代码?不求最短,而是最易读的设为最佳。import cv2
#打开标签
#这版尝试保存整张图分成多张图后的标签和图片
f = open('.\\label\\000006.txt')
f1 = open('10000061.txt','w+')
f2 = open('10000062.txt','w+')
f3 = open('10000063.txt','w+')
#读取标签中目标的坐标
data = []
newdata = []
le =[]
ri=[]
for line in f.readlines():
temp = line.split()
data.append(temp)
newdata = data.copy()
#这一段是裁剪图片的代码
img = cv2.imread('000006.png',1)
a=0
b = 370
height, width, depth = img.shape
for i in range(0,len(data)):
xmin = int(float(data)*width - 0.5*float(data)*width)
le.append(xmin)
xmax = int(float(data)*width + 0.5*float(data)*width)
ri.append(xmax)
if b in range(le,ri):
b = ri + 10
else:
pass
if i == len(data)-1:
dst1 = img
#换成保存图片
cv2.imwrite('.\\save\\10000001'+'.png',dst1)
print(data)
for i in range(0,len(data)):
newdata=(((le+ri)/2)-a)/(b-a)
print(data)
newdata=float(newdata)*width/(b-a)
for j in range(0,len(ri)):
if ri in range(a,b):
for p in range(len(newdata)):
f1.write(str(newdata)+' ')
f1.write('\n')
f1.close()
print(newdata)
print(data)
a = b
b = b +370
le =[]
ri=[]
for i in range(0,len(data)):
xmin = int(float(data)*width - 0.5*float(data)*width)
le.append(xmin)
xmax = int(float(data)*width + 0.5*float(data)*width)
ri.append(xmax)
if b in range(le,ri):
b = ri + 10
else:
pass
if i == len(data)-1:
dst1 = img
#换成保存图片
cv2.imwrite('.\\save\\10000002'+'.png',dst1)
for i in range(0,len(data)):
newdata=(((le+ri)/2)-a)/(b-a)
newdata=float(data)*width/(b-a)
for j in range(0,len(ri)):
if ri in range(a,b):
for p in range(len(newdata)):
f2.write(str(newdata)+' ')
f2.write('\n')
f2.close()
a = b
b = b +370
le =[]
ri=[]
for i in range(0,len(data)):
xmin = int(float(data)*width - 0.5*float(data)*width)
le.append(xmin)
xmax = int(float(data)*width + 0.5*float(data)*width)
ri.append(xmax)
if b in range(le,ri):
b = ri + 10
else:
pass
if i == len(data)-1:
dst1 = img
#换成保存图片
cv2.imwrite('.\\save\\10000003'+'.png',dst1)
for i in range(0,len(data)):
newdata=(((le+ri)/2)-a)/(b-a)
newdata=float(data)*width/(b-a)
for j in range(0,len(ri)):
if ri in range(a,b):
for p in range(len(newdata)):
f3.write(str(newdata)+' ')
f3.write('\n')
f3.close()
你这也不是悬赏啊怎么设置最佳?
页:
[1]