字典保存json dumps

def  test():
    # 计算每个id 的bgr平均值 ,查看每种变换颜色风格  bgr是什么数值区间,平均值和最大最小值
    filename = '/home/shiyy/nas/pcl_ai/train/me_id_bgr.json'

    # 倒着数第四位'.'为分界线,按照‘.’左边的数字从小到大排序,顺序显示
    allfile = os.listdir("/home/shiyy/nas/pcl_ai/train/me_id_files")
    allfile.sort(key=lambda x: int(x.split('.')[0]))
    for id_img in allfile[0:100]:
        lists = os.listdir("/home/shiyy/nas/pcl_ai/train/me_id_files/" + id_img)
        Bmean=[]
        Gmean=[]
        Rmean=[]
        Bmax = []
        Gmax = []
        Rmax = []
        Bmin = []
        Gmin = []
        Rmin = []
        for im in lists:
            img = cv2.imread(os.path.join(root, im))
            per_image_Bmean = np.mean(img[:, :, 0])
            per_image_Gmean = np.mean(img[:, :, 1])
            per_image_Rmean = np.mean(img[:, :, 2])

            per_image_Bmax = np.max(img[:, :, 0])
            per_image_Gmax = np.max(img[:, :, 1])
            per_image_Rmax = np.max(img[:, :, 2])

            per_image_Bmin = np.min(img[:, :, 0])
            per_image_Gmin = np.min(img[:, :, 1])
            per_image_Rmin = np.min(img[:, :, 2])


            Bmean.append(per_image_Bmean)
            Gmean.append(per_image_Gmean)
            Rmean.append(per_image_Rmean)

            Bmax.append(per_image_Bmax)
            Gmax.append(per_image_Gmax)
            Rmax.append(per_image_Rmax)

            Bmin.append(per_image_Bmin)
            Gmin.append(per_image_Gmin)
            Rmin.append(per_image_Rmin)


        Bmean = np.mean(np.array(Bmean))
        Gmean = np.mean(np.array(Gmean))
        Rmean = np.mean(np.array(Rmean))

        Bmax = np.mean(np.array(Bmax))
        Gmax = np.mean(np.array(Gmax))
        Rmax = np.mean(np.array(Rmax))

        Bmin = np.mean(np.array(Bmin))
        Gmin = np.mean(np.array(Gmin))
        Rmin = np.mean(np.array(Rmin))

        print("id 是 ",id_img,"的bgr 通道平均值,最大最小值")
        print("bgr均值:",(Bmean),(Gmean),(Rmean))
        print("b最大最小值",Bmax,Bmin)
        print("g最大最小值",Gmax,Gmin)
        print("r最大最小值",Rmax,Rmin)

        items={"id ":id_img,
               "bgr mean":(Bmean,Gmean,Rmean), #中文乱码
               "b max min ":(Bmax,Bmin),
               "g max min ": (Gmax,Gmin),
               "r max min ": (Rmax,Rmin)
               }


        items= json.dumps(items)
        with open(filename, "a", encoding='utf-8') as f:#字典保存json #需要删除再追加
            f.write(items + ",\n")

你可能感兴趣的:(字典保存json dumps)