国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

利用python將圖片轉換成excel文檔格式

2020-02-16 11:22:52
字體:
來源:轉載
供稿:網友

前言

本文主要介紹了關于利用python將圖片轉換成excel文檔的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。

實現步驟

讀取圖像,獲取圖像每個像素點的RGB值; 根據每個像素點的RGB值設置excel每個方格的顏色值; 根據像素點的坐標,寫入excel文件; 保存退出;

示例代碼

from PIL import Imageimport numpy as npimport timeimport matplotlib.pyplot as pltimport xlsxwriterdef 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)

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林站長站的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江门市| 丰原市| 中江县| 大理市| 射阳县| 肇源县| 双桥区| 泰州市| 姜堰市| 房产| 土默特右旗| 清水县| 米泉市| 舟曲县| 大关县| 蒲江县| 巨野县| 土默特右旗| 健康| 姜堰市| 闽侯县| 澎湖县| 昂仁县| 资兴市| 恭城| 育儿| 山东| 延长县| 武安市| 教育| 资源县| 伊宁市| 阳谷县| 墨江| 平和县| 许昌县| 通渭县| 莎车县| 承德市| 独山县| 定日县|