利用python將圖片轉(zhuǎn)換成excel文檔格式
更新時間:2017年12月30日 10:02:11 作者:神奇的戰(zhàn)士
編寫了一小段Python代碼,將圖片轉(zhuǎn)為了Excel,純屬娛樂,下面這篇文章主要給大家介紹了關(guān)于利用python將圖片轉(zhuǎn)換成excel文檔格式的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
前言
本文主要介紹了關(guān)于利用python將圖片轉(zhuǎn)換成excel文檔的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
實現(xiàn)步驟
- 讀取圖像,獲取圖像每個像素點的RGB值;
- 根據(jù)每個像素點的RGB值設(shè)置excel每個方格的顏色值;
- 根據(jù)像素點的坐標(biāo),寫入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, 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)
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
您可能感興趣的文章:
- python實現(xiàn)圖片轉(zhuǎn)換成素描和漫畫格式
- Python3.7將普通圖片(png)轉(zhuǎn)換為SVG圖片格式(網(wǎng)站logo圖標(biāo))動起來
- Python 實現(xiàn)判斷圖片格式并轉(zhuǎn)換,將轉(zhuǎn)換的圖像存到生成的文件夾中
- Python 實現(xiàn)opencv所使用的圖片格式與 base64 轉(zhuǎn)換
- 利用python和ffmpeg 批量將其他圖片轉(zhuǎn)換為.yuv格式的方法
- python將.ppm格式圖片轉(zhuǎn)換成.jpg格式文件的方法
- 使用python將圖片格式轉(zhuǎn)換為ico格式的示例
- Python3實現(xiàn)轉(zhuǎn)換Image圖片格式
- python實現(xiàn)批量圖片格式轉(zhuǎn)換
- Python將圖片批量從png格式轉(zhuǎn)換至WebP格式
- python實現(xiàn)通過pil模塊對圖片格式進行轉(zhuǎn)換的方法
- python利用tkinter實現(xiàn)圖片格式轉(zhuǎn)換的示例
相關(guān)文章
Python數(shù)據(jù)分析:手把手教你用Pandas生成可視化圖表的教程
今天小編就為大家分享一篇Python數(shù)據(jù)分析:手把手教你用Pandas生成可視化圖表的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
python Pandas中數(shù)據(jù)的合并與分組聚合
大家好,本篇文章主要講的是python Pandas中數(shù)據(jù)的合并與分組聚合,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01

