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


本文摘自php中文网,作者小云云,侵删。

本文主要介绍了关于利用python将图片转换成excel文档的相关内容,编写了一小段Python代码,将图片转为了Excel,纯属娱乐,下面这篇文章主要给大家介绍了关于利用python将图片转换成excel文档格式的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。

实现步骤

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

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

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

  • 保存退出;

示例代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

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, j, 1]), hex(img[i, j, 2])

   cellcolor = (hex(img[i, j, 0]) + hex(img[i, j, 1]) + hex(img[i, j, 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)

相关推荐:

详解C#winform打开Excel文档的方法总结

通过php实现获取excel文档内容的代码实例

如何使用php生成EXCEL文档

以上就是利用python将图片转换成excel文档格式详解的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python 的 help 函数使用

Python装饰器之property()教程详解

Python中type()是什么意思

浅谈Python字符串

Python的pil库如何安装

2018年最火的七个Python图形化gui开发框架

Python中if语句用法

电脑64位怎么下载Python

Python如何将字母转化为数字

Python中json模块和pickle模块的简单介绍(附示例)

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...