0
点赞
收藏
分享

微信扫一扫

python将字典写入txt文件并读出来

栖桐 2022-04-29 阅读 56
python
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')
# 转化为PIL格式 保存
Image.fromarray(img).save(img_path)
举报

相关推荐

0 条评论