python opencv批量修改图片格式

 

 

#coding=utf-8
import cv2
import os

path='/home/fhy145/workspace/bm2/dataset/winter/bgr1/'
img_paths=os.listdir(path)
#print(img_paths)
# img_paths.sort(key=lambda x: int(x[:-4]))  # 倒着数第四位'.'为分界线,按照‘.'左边的数字从小到大排序
# print(img_paths)
path_obj='/home/fhy145/workspace/bm2/dataset/winter/bgr2/'
 
# 判断结果
if not os.path.exists(path_obj):
    # 如果不存在则创建目录
    # 创建目录操作函数
    os.makedirs(path_obj)
    print(path_obj," create over!")
else:
    print(path_obj," exist!")

i=0
for img in img_paths:
    i+=1
    abs_path = os.path.join(path,img)
    img_name=img.split('.')[0]
    print(img_name)
    img_mat = cv2.imread(abs_path)
    # cv2.imshow("img",img_mat)
    # cv2.waitKey(1000)
    cv2.imwrite(path_obj+img_name+".jpg",img_mat)
    print(i)

 

上一篇:不同路径


下一篇:Android 框架实现分析 - 工具 - host工具