利用python將圖片轉(zhuǎn)換成excel文檔格式
前言
本文主要介紹了關(guān)于利用python將圖片轉(zhuǎn)換成excel文檔的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
實(shí)現(xiàn)步驟
- 讀取圖像,獲取圖像每個(gè)像素點(diǎn)的RGB值;
- 根據(jù)每個(gè)像素點(diǎn)的RGB值設(shè)置excel每個(gè)方格的顏色值;
- 根據(jù)像素點(diǎn)的坐標(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)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- python實(shí)現(xiàn)圖片轉(zhuǎn)換成素描和漫畫格式
- Python3.7將普通圖片(png)轉(zhuǎn)換為SVG圖片格式(網(wǎng)站logo圖標(biāo))動(dòng)起來
- Python 實(shí)現(xiàn)判斷圖片格式并轉(zhuǎn)換,將轉(zhuǎn)換的圖像存到生成的文件夾中
- Python 實(shí)現(xiàn)opencv所使用的圖片格式與 base64 轉(zhuǎn)換
- 利用python和ffmpeg 批量將其他圖片轉(zhuǎn)換為.yuv格式的方法
- python將.ppm格式圖片轉(zhuǎn)換成.jpg格式文件的方法
- 使用python將圖片格式轉(zhuǎn)換為ico格式的示例
- Python3實(shí)現(xiàn)轉(zhuǎn)換Image圖片格式
- python實(shí)現(xiàn)批量圖片格式轉(zhuǎn)換
- Python將圖片批量從png格式轉(zhuǎn)換至WebP格式
- python實(shí)現(xiàn)通過pil模塊對(duì)圖片格式進(jìn)行轉(zhuǎn)換的方法
- python利用tkinter實(shí)現(xiàn)圖片格式轉(zhuǎn)換的示例
相關(guān)文章
Python中橫向或縱向拼接兩個(gè)表方法實(shí)例
最近要將兩個(gè)表格合并,Python處理起來很簡單,所以這篇文章主要給大家介紹了關(guān)于Python中橫向或縱向拼接兩個(gè)表的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07Python?Ruby?等語言棄用自增運(yùn)算符原因剖析
這篇文章主要為大家介紹了Python?Ruby?等語言棄用自增運(yùn)算符原因剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Python數(shù)據(jù)分析:手把手教你用Pandas生成可視化圖表的教程
今天小編就為大家分享一篇Python數(shù)據(jù)分析:手把手教你用Pandas生成可視化圖表的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python實(shí)現(xiàn)自動(dòng)整理文件的腳本
這篇文章主要介紹了Python實(shí)現(xiàn)自動(dòng)整理文件的腳本,幫助大家更好的利用python處理文件,感興趣的朋友可以了解下2020-12-12python Pandas中數(shù)據(jù)的合并與分組聚合
大家好,本篇文章主要講的是python Pandas中數(shù)據(jù)的合并與分組聚合,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01