python xlwt 写入数据到excel

声明

  • xlwt是python中写入到excel的库
  • 仅支持写入到xls文件
  • 支持最大写入65535行,超过会报错。此时建议使用openpyxl,详见我的另一篇博客:python openpyxl 读写excel

具体步骤

  • 第一步,新建表格
  • 第二步,新建工作表sheet
  • 第三步,写入数据,给定行列号和值
  • 第四步,保存表格
import cv2
import numpy as np
from xlwt import *

txtpath = r'a.txt'

# 新建表格
file = Workbook(encoding='utf-8')
# 新建工作表sheet
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'))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值