import os
import cv2
def sample(fil_path, sav_path, sampling_num):
file_list = os.listdir(fil_path) # 获取文件列表
print("文件的个数为:", len(file_list))
for item in file_list:
if item.endswith('.jpg'):
path = os.path.join(os.path.abspath(fil_path), item) # 添加路径以及图片的名字
img = cv2.imread(path) # 读取图片 必须加上路径,否则图片加载容易出问题
for i in range(sampling_num):
img = cv2.pyrUp(img) # 上采样
# img = cv2.pyrDown(img) # 下采样
# 第一个参数是保存的路径,第二个参数是图片文件,第三个是特定格式的质量,这里是JPEG格式的,0-100是质量,100最好
cv2.imwrite(sav_path + str(item), img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
else:
print('not ok')
cv2.imshow('test', img) # 第一个字符是在窗口显示的字符,第二个字符是显示的源
cv2.waitKey(0)
if __name__ == '__main__':
file_path = 'F:/2022test/CE_5_com/photo_label/JPEGImages/'
save_path = 'F:/2022test/CE_5_com/photo_label/save_JPEGImages/'
sampling_num = 1 # 上采样的次数 1:2倍上采样,2:4倍上采样,3:8倍上采样
sample(file_path, save_path, sampling_num)