声明
具体步骤
- 第一步,新建表格
- 第二步,新建工作表sheet
- 第三步,写入数据,给定行列号和值
- 第四步,保存表格
import cv2
import numpy as np
from xlwt import *
txtpath = r'a.txt'
file = Workbook(encoding='utf-8')
table = file.add_sheet('hwrgb')
table.write(0, 0, 'height')
table.write(0, 1, 'width')
table.write(0, 2, 'channnel')
table.write(0, 3, 'red')
table.write(0, 4, 'green')
table.write(0, 5, 'blue')
num = 1
for line in open(txtpath):
line = line.strip()
line_split = line.split(' ')
image_path = line_split[0]
img = cv2.imread(image_path, cv2.COLOR_BGR2RGB)
height, width, channnel = img.shape[0], img.shape[1], img.shape[2]
img_mean = np.mean(img, axis=(0, 1))
table.write(num, 0, height)
table.write(num, 1, width)
table.write(num, 2, channnel)
table.write(num, 3, img_mean[0])
table.write(num, 4, img_mean[1])
table.write(num, 5, img_mean[2])
num += 1
file.save('{}.xls'.format(r'D:\defect_segmentation\DataSet\hw_rgb'))