# -*- coding: utf-8 -*-
"""
@author: yangsong
@file: ImgToCsv.py
@time: 2019-3-14 11:56
@desc:
@version: 1.0
"""
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
if int(num1) == 0:
return table[num2 - 1] + str(row)
else:
return table[int(num1) - 1] + table[num2 - 1] + str(row)
def main():
img = np.array(Image.open('1.jpg'))
# plt.figure(figsize=(100, 100))
# plt.imshow(img)
# plt.show()
rows, cols, dims = img.shape
print(img.shape)
print(img.dtype)
print(img.size)
print(type(img))
# plt.imshow(img)
# plt.show()
# print img[188, 188, 0]
excel = xlsxwriter.Workbook('1.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):
cellcolor = (hex(img[i, j, 0]) + hex(img[i, j, 1]) + hex(img[i, j, 2])).replace('0x', '')
cellformat = excel.add_format({'bg_color': '#'+cellcolor,
'font_color': '#'+cellcolor})
worksheet1.conditional_format(get_xy(i, j), {'type': 'cell',
'criteria': '<',
'value': 50,
'format': cellformat})
excel.close()
if __name__ == '__main__':
main()
利用Python实现:将图片转化为Excel并保存
最新推荐文章于 2024-08-19 03:11:16 发布