python学习
2021-01-28
- 以numpy数组格式读取图片和txt文件
1 import numpy as np 2 import cv2 as cv 3 4 root = "000050" 5 6 img_path = "./train/images/" + root + ".jpg" 7 txt_path = "./train/labels/" + root + ".txt" 8 9 src=cv.imread(img_path) 10 11 loc = np.loadtxt(txt_path) 12 13 cv.namedWindow('input_image', cv.WINDOW_AUTOSIZE) 14 cv.imshow('input_image', src) 15 cv.waitKey(0) 16 cv.destroyAllWindows()
- 写txt文件
f = open('./data/data_classes.txt', 'w') j = 1 for i in a: f.write(' \"'+i+'\": '+str(j)+',\n') j += 1 f.close()
- 图像缩放,读取文件夹下所有文件名
from PIL import Image path = '***' fileList = os.listdir(path) for img in fileList: #读取图像 root = path + "/" + img im = Image.open(root) #im.show() #原图像缩放为128x128 im_resized = im.resize((512, 384)) #im_resized.show() root = "./resize_qdu_dataset/val/images/" + img im_resized.save(root)