python将字典写入txt文件并读出来、保存图像、resize窗口展示图像、读yaml配置文件

将字典写入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)  # 连线
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1900_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值