# 读取csv文件并输出到指定文件夹
import os
import glob
import re
import csv
import math
import cv2 as cv
import numpy as np
def drawpicture():
# file_path = 'F:\\zhengwangwork\\data\\csv\\接头、螺孔、孔裂\\csv' #读取csv文件的位置
file_path = 'F:\\testcsv\\csv' # 读取csv文件的位置
files = glob.glob(os.path.join(file_path, "*.csv"))
for file2 in files:
# (233,1600,3)分别是宽、高、通道数
canvas = np.zeros((233, 1100, 3), dtype=np.uint8)
# file = open('F:/data/BJYjoint/csvFile/2.csv') # 打开csv文件
file = open(file2) # 打开csv文件
reader = csv.reader(file) # 读取csv文件
data = list(reader) # 将csv数据转化为列表
length_h = len(data) # 得到数据行数
lenght_l = len(data[0]) # 得到每行长度
x = list()
x1 = list()
y = list()
z = list()
# 绘制散点图
for i in range(0, length_h): # 从第一行开始读取
x1.append(float(eval(data[i][0]))) # 将第一列数据从第一行读取到最后一行付给列表x
y.append(int(eval(data[i][2]))) # 将第三列数据从第一行读取到最后一行付给列表y
z.append(eval(data[i][1]))
a = max(x1) - min(x1)
if a == 0:
pass
elif (a > 1080):
b = 1080 / a
else:
b = 1
for i in range(0, length_h):
x.append(int(x1[i] * b + 7))
pts = zip(x, y)
col = []
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])
print(x, y, x1, y1)
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])
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
print(i)
n = os.path.basename(file2)
a = n.replace(".csv", ".png")
# str2 = "F:/png1/" + a #png图片存储的位置
str2 = "F:\\testpng2\\" + a # png图片存储的位置
cv.imwrite(str2, canvas)
cv.waitKey()
if __name__ == '__main__':
drawpicture()
python处理csv文件进行绘图
最新推荐文章于 2024-05-30 10:12:06 发布