python将excel转换成图片_利用python将图片转换成excel文档格式

前言

本文主要介绍了关于利用python将图片转换成excel文档的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

实现步骤

读取图像,获取图像每个像素点的RGB值;

根据每个像素点的RGB值设置excel每个方格的颜色值;

根据像素点的坐标,写入excel文件;

保存退出;

示例代码

from PIL import Image

import numpy as np

import time

import matplotlib.pyplot as plt

import xlsxwriter

def get_xy(row,col):

table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

num1 = col / 26

num2 = col % 26

# print num1,num2

if num1 == 0:

return table[num2 - 1] + str(row)

else:

return table[num1-1] + table[num2 - 1] + str(row)

def main():

img = np.array(Image.open('whale.jpeg'))

# plt.figure("whale")

# plt.imshow(img)

# plt.show()

rows,cols,dims = img.shape

print img.shape

print img.dtype

print img.size

print type(img)

# print img[188,188,0]

excel = xlsxwriter.Workbook('image_excel.xlsx')

cellformat = excel.add_format({'bg_color': '#123456','font_color': '#654321'})

worksheet1 = excel.add_worksheet()

data = []

color = [''] * cols

cellcolor = ""

for i in range(rows):

for j in range(cols):

# print hex(img[i,j,0]),hex(img[i,1]),2])

cellcolor = (hex(img[i,0]) + hex(img[i,1]) + hex(img[i,2])).replace('0x','')

# print cellcolor

cellformat = excel.add_format({'bg_color': '#'+cellcolor,'font_color': '#'+cellcolor})

# cellformat = excel.add_format({'bg_color': '#C6EFCE',# 'font_color': '#006100'})

worksheet1.conditional_format(get_xy(i,j),{'type': 'cell','criteria': '<','value': 50,'format': cellformat})

# data.append(data_row)

excel.close()

if __name__ == '__main__':

main()

# print get_xy(133,27)

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对编程小技巧的支持。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值