先读取所有的图片数据
root = './new_negs/'
def show_files(path, all_files):
# 首先遍历当前目录所有文件及文件夹
file_list = os.listdir(path)
# 准备循环判断每个元素是否是文件夹还是文件,是文件的话,把名称传入list,是文件夹的话,递归
for file in file_list:
# 利用os.path.join()方法取得路径全名,并存入cur_path变量,否则每次只能遍历一层目录
cur_path = os.path.join(path, file)
# 判断是否是文件夹
if os.path.isdir(cur_path):
show_files(cur_path, all_files)
elif cur_path.endswith('.jpg'):
all_files.append(cur_path)
return all_files
对每张图片进行操作
index = 0
while index < len(contents):
content = contents[index]
print('processing {}: '.format(content), end='')
img = cv2.imread(content)
cv2.imshow('img', img)
i = cv2.waitKey(0)
img_name = content.split('/')[-1]
if i == ord('u'):
new_dir = 'uncertain'
elif i == ord('b'): # backwards
print('no move')
index -= 1
continue
else: # forwards
new_dir = 'new_negs'
dst = os.path.join(new_dir, img_name)
shutil.move(content, dst)
contents[index] = dst
print('moved to {}'.format(new_dir))
index += 1
同时查看多张图片
root_auth = '../outputs1'
contents_auth = [os.path.join(dp, fn) for dp, _, fns in os.walk(root_auth) for fn in fns if '.jpg' in fn]
contents_auth.sort()
root_self = '../outputs'
contents_self = [os.path.join(dp, fn) for dp, _, fns in os.walk(root_self) for fn in fns if '.jpg' in fn]
contents_self.sort()
print('{} images need to be done.'.format(min(len(contents_auth), len(contents_self))))
idx = 0
while idx < min(len(contents_auth), len(contents_self)):
content_self = contents_self[idx]
title = content_self.split('/')[-1]
content_auth = os.path.join(root_auth, title)
if content_auth not in contents_auth:
idx += 3
continue
print('processing {}: '.format(content_auth), end='')
img_auth = cv2.imread(content_auth)
scale = 500 / img_auth.shape[0]
img_auth = cv2.resize(img_auth, (0, 0), fx=scale, fy=scale)
img_self = cv2.imread(content_self)
img_self = cv2.resize(img_self, (0, 0), fx=scale, fy=scale)
imgs = np.concatenate([img_auth, img_self]) # 垂直显示
# imgs = np.hstack([img_auth, img_self]) 水平显示
cv2.imshow('img', imgs)
i = cv2.waitKey(0)
img_name = content_auth.split('/')[-1]
if i == ord('d'):
idx -= 1
continue
else:
idx += 3
- 在图片上添加矩形框和文字
img = cv2.imread(img_path)
# (img, 左上角,右下角,color,宽度)
cv2.rectangle(img, (10,50), (50,100), (0,255,0), 4)
font = cv2.FONT_HERSHEY_SIMPLEX
text = 'python'
# (img,text,开始坐标,字体font,字体大小,color,字体粗细)
cv2.putText(img, text, (50, 50), font, 1, (0,0,255), 1)
cv2读取图片的格式为:HWC,其余都是:CHW,故使用transpose((2,0,1)),使图片变成我们需要的格式。
还有其它的方式转换图片格式:img[:,:,::-1]对应H、W、C,彩图是3通道,即C是3层。opencv里对应BGR,故通过C通道的 ::-1 就是把BGR转为RGB。
img = torch.from_numpy(img.transpose(2, 0, 1)).float().div(255.0).unsqueeze(0)
- opencv无损存图
0代表图片保存时的压缩程度,有0-9这个范围的10个等级,数字越大表示压缩程度越高
cv2.imwrite("img_dir", img, [cv2.IMWRITE_PNG_COMPRESSION, 0])