将字典写入txt文件,并且命名为当前时间
读入该txt文本
读入图像,并保存为PIL格式
from PIL import Image
import os
import time
import cv2
s ={
1:[1,2,3],
2:[1,2,3],
3:[1,2,3],
4:[1,2,3]
}
# save txt
save_dir = './'
# get time
save_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime())
txt_path = os.path.join(save_dir, '{}.txt'.format(save_time))
# write txt
fs = open(txt_path,'w')
fs.write(str(s))
fs.close()
# read txt
fs = open(txt_path,'r+')
test_read = eval(fs.read() )
fs.close()
# 打印看下
print(test_read)
# save img
# 设置保存图像的路径
img_path = os.path.join(save_dir, '{}.jpg'.format(save_time))
# 读进来一个图像
img = cv2.imread('5.jpg')
#直接保存
cv2.imwrite("E:\\project\\pytorch-Project\\test_img\\img_1_1.jpg",img)
# 转化为PIL格式 保存
Image.fromarray(img).save(img_path)
展示图像的时候,resize窗口大小
w_w = im0.shape[1]
h_h = im0.shape[0]
resize = cv2.resize(im0, (int(w_w / 2), int(h_h / 2)), interpolation=cv2.INTER_CUBIC)
cv2.imshow(str(p), resize)
cv2.waitKey(0)
为程序写yaml配置文件
写好的yaml配置文件与主程序位于同级目录
在主程序内引入
yaml_path = 'config.yaml' # 这是路径
with open(yaml_path,'r') as yaml_file:
config = yaml.load(yaml_file.read(),Loader=yaml.FullLoader)
然后就可以在主程序内使用config['']
来访问配置文件中的配置变量了
画点
cv2.circle(im0, (x, y), 10, (255,255,255), -1)
画线:
cv2.line(img, (x1,y1), (x2,y2), (0, 255, 0), 2) # 连线