搜尋
首頁後端開發Python教學利用python將圖片轉換成excel文件格式詳解

本文主要介紹了關於利用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, 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': '<p>相關推薦:<br></p><p><a href="http://www.php.cn/csharp-article-358580.html" target="_self">詳情C#winform開啟Excel文件的方法總結</a></p><p><a href="http://www.php.cn/php-weizijiaocheng-341617.html" target="_self">透過php實作取得excel文件內容的程式碼實例</a></p><p><a href="http://www.php.cn/php-weizijiaocheng-333263.html" target="_self">#如何使用php產生EXCEL文件</a></p>

以上是利用python將圖片轉換成excel文件格式詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
對於循環和python中的循環時:每個循環的優點是什麼?對於循環和python中的循環時:每個循環的優點是什麼?May 13, 2025 am 12:01 AM

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

Python:深入研究彙編和解釋Python:深入研究彙編和解釋May 12, 2025 am 12:14 AM

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

Python是一種解釋或編譯語言,為什麼重要?Python是一種解釋或編譯語言,為什麼重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

對於python中的循環時循環與循環:解釋了關鍵差異對於python中的循環時循環與循環:解釋了關鍵差異May 12, 2025 am 12:08 AM

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

循環時:實用指南循環時:實用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

Python:它是真正的解釋嗎?揭穿神話Python:它是真正的解釋嗎?揭穿神話May 12, 2025 am 12:05 AM

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

與同一元素的Python串聯列表與同一元素的Python串聯列表May 11, 2025 am 12:08 AM

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

解釋與編譯語言:Python的位置解釋與編譯語言:Python的位置May 11, 2025 am 12:07 AM

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境