python的读取文件夹下的csv并将生成图片存放到指定路径

import os
import glob
import re
import csv
import math

import cv2 as cv
import numpy as np


def drawpicture():
    file_path = 'F:/data/BJYjoint/csvFile'
    files = glob.glob(os.path.join(file_path, "*.csv"))
    for file2 in files:
        # (233,1600,3)分别是宽、高、通道数
        canvas = np.zeros((233, 1600, 3), dtype=np.uint8)
        # file = open('F:/data/BJYjoint/csvFile/2.csv')  # 打开csv文件
        file = open(file2)  # 打开csv文件
        # print(file)
        reader = csv.reader(file)  # 读取csv文件
        data = list(reader)  # 将csv数据转化为列表
        length_h = len(data)  # 得到数据行数
        lenght_l = len(data[0])  # 得到每行长度
        x = list()
        y = list()
        z = list()
        # 绘制散点图
        for i in range(0, length_h):  # 从第一行开始读取
            x.append(int(eval(data[i][0])) + 7)  # 将第一列数据从第一行读取到最后一行付给列表x
            y.append(eval(data[i][2]))  # 将第三列数据从第一行读取到最后一行付给列表y
            z.append(eval(data[i][1]))
        pts = zip(x, y)
        col = []
        print(type(z[1]))
        dict = {"0": (0, 255, 255), "1": (0, 0, 255), "2": (255, 192, 255), "3": (255, 127, 255), "4": (255, 64, 255),
                "5": (127, 255, 0),
                "6": (127, 0, 255), "7": (64, 0, 255), "8": (0, 255, 0), "9": (0, 127, 0), "10": (255, 192, 0),
                "11": (255, 127, 0), "12": (255, 64, 0), "13": (127, 55, 255), "14": (127, 0, 0), "15": (64, 0, 0),
                "16": (0, 0, 127), "17": (255, 0, 255), "18": (255, 0, 0), " 19": (127, 240, 0), "20": (127, 240, 255),
                "21": (255, 15, 255), "22": (255, 15, 0), "23": (0, 0, 120), "24": (255, 255, 255), "25": (255, 255, 0),
                " 26": (255, 240, 255), "27": (255, 240, 0)}
        # # 通过循环将不同的通道值对应的颜色加到col数组中,其中data1就是db数组
        for i in range(0, len(z)):
            col.append(dict[str(z[i])])
        i = 0
        for pt in pts:
            # 前项45度(绿色的)
            if z[i] == 5 or z[i] == 6 or z[i] == 19:
                angel = 37.5
                angelPi = (angel / 180) * math.pi
                x = int(4 * math.cos(angelPi) + pt[0])
                y = int(4 * math.sin(angelPi) + pt[1])
                x1 = int(-4 * math.cos(angelPi) + pt[0])
                y1 = int(-4 * math.sin(angelPi) + pt[1])
                # print(x, y)
                # print(x1, y1)
                cv.line(canvas, (x, y), (x1, y1), col[i], thickness=2)
            # 后向45度
            elif z[i] == 13 or z[i] == 14 or z[i] == 20:
                angel = 37.5
                angelPi = (angel / 180) * math.pi
                x = int(4 * math.cos(angelPi) + pt[0])
                y = int(-4 * math.sin(angelPi) + pt[1])
                x1 = int(-4 * math.cos(angelPi) + pt[0])
                y1 = int(4 * math.sin(angelPi) + pt[1])
                cv.line(canvas, (x, y), (x1, y1), col[i], thickness=2)
            # 前项75度
            elif z[i] == 2 or z[i] == 3 or z[i] == 4 or z[i] == 7 or z[i] == 17 or z[i] == 21 or z[i] == 24 or z[
                i] == 26:
                angel = 67.5
                angelPi = (angel / 180) * math.pi
                x = int(4 * math.cos(angelPi) + pt[0])
                y = int(-4 * math.sin(angelPi) + pt[1])
                x1 = int(-4 * math.cos(angelPi) + pt[0])
                y1 = int(4 * math.sin(angelPi) + pt[1])
                # print(x, y)
                # print(x1, y1)
                cv.line(canvas, (x, y), (x1, y1), col[i], thickness=2)


            # 后项75度
            elif z[i] == 10 or z[i] == 11 or z[i] == 12 or z[i] == 15 or z[i] == 18 or z[i] == 22 or z[i] == 25 or z[
                i] == 27:
                angel = 67.5
                angelPi = (angel / 180) * math.pi
                x = int(4 * math.cos(angelPi) + pt[0])
                y = int(4 * math.sin(angelPi) + pt[1])
                x1 = int(-4 * math.cos(angelPi) + pt[0])
                y1 = int(-4 * math.sin(angelPi) + pt[1])
                cv.line(canvas, (x, y), (x1, y1), col[i], thickness=2)

            # 0度[0,1,8,9,16,23]
            else:
                x = (pt[0] - 4, pt[1])
                y = (pt[0] + 4, pt[1])
                cv.line(canvas, x, y, col[i], thickness=2)
            i = i + 1
        # 展示绘制的图形
        #   cv.namedWindow('1', cv.WINDOW_NORMAL)

        # cv.imshow('1', canvas)
        # 保存绘制的图像

        # print(str(file2))
        # print(str(re.findall("\d+",file2)))
        n = str(re.findall("\d+", file2))  # 提取字符串中的数字
        a = re.sub('([^\u4e00-\u9fa5\u0030-\u0039])', '', n)  # 只保留汉字和数字
        str2 = "F:/png/" + a

        cv.imwrite(str2 + '.png', canvas)

        cv.waitKey()


if __name__ == '__main__':
    drawpicture()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值