自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

神操作!居然有人用 Python 在 Excel 中畫(huà)畫(huà)

開(kāi)發(fā) 后端
用 Python 讀取圖片的像素值,然后輸出到 Excel 表格中,最終形成一幅像素畫(huà),也就是電子版的十字繡了。

十字繡大家都知道吧,今天咱們來(lái)玩?zhèn)€電子版的十字繡。

用 Python 讀取圖片的像素值,然后輸出到 Excel 表格中,最終形成一幅像素畫(huà),也就是電子版的十字繡了。

準(zhǔn)備

既然要讀取圖片,那就需要用到 Pillow 庫(kù),操作 Excel 需要用到 openpyxl 庫(kù),先把這兩個(gè)庫(kù)安裝好。

  1. $ pip3 install openpyxl 
  2. $ pip3 install Pillow 

色值轉(zhuǎn)換

從圖片讀取的像素塊色值是 RGB 值,而 openpyxl 向 Excel cell 內(nèi)填充顏色是十六進(jìn)制色值,因此咱們先寫(xiě)一個(gè) RGB 和十六進(jìn)制色值轉(zhuǎn)換的一個(gè)函數(shù)。

  1. def rgb_to_hex(rgb): 
  2.     rgb = rgb.split(','
  3.     color = '' 
  4.     for i in RGB: 
  5.         num = int(i) 
  6.         color += str(hex(num))[-2:].replace('x''0').upper() 
  7.     return color 

圖片轉(zhuǎn)換

有了色值轉(zhuǎn)換函數(shù),接下來(lái)要做的操作就是逐行讀取圖片的 RGB 色值,之后將 RGB 色值轉(zhuǎn)換為十六進(jìn)制色值填充到 Excel 的 cell 中即可。

  1. def img2excel(img_path, excel_path): 
  2.     img_src = Image.open(img_path) 
  3.     # 圖片寬高 
  4.     img_width = img_src.size[0] 
  5.     img_height = img_src.size[1] 
  6.  
  7.     str_strlist = img_src.load() 
  8.     wb = openpyxl.Workbook() 
  9.     wb.save(excel_path) 
  10.     wb = openpyxl.load_workbook(excel_path) 
  11.     cell_width, cell_height = 1.0, 1.0 
  12.  
  13.     sheet = wb["Sheet"
  14.     for w in range(img_width): 
  15.         for h in range(img_height): 
  16.             data = str_strlist[w, h] 
  17.             color = str(data).replace("(""").replace(")"""
  18.             color = rgb_to_hex(color) 
  19.             # 設(shè)置填充顏色為 color 
  20.             fille = PatternFill("solid", fgColor=color) 
  21.             sheet.cell(h + 1, w + 1).fill = fille 
  22.     for i in range(1, sheet.max_row + 1): 
  23.         sheet.row_dimensions[i].height = cell_height 
  24.     for i in range(1, sheet.max_column + 1): 
  25.         sheet.column_dimensions[get_column_letter(i)].width = cell_width 
  26.     wb.save(excel_path) 
  27.     img_src.close() 

最后再來(lái)個(gè)入口函數(shù),就大功告成啦~

  1. if __name__ == '__main__'
  2.     img_path = '/Users/xyz/Documents/tmp/03.png' 
  3.     excel_path = '/Users/xyz/Documents/tmp/3.xlsx' 
  4.     img2excel(img_path, excel_path) 

驚艷時(shí)刻

激動(dòng)的心,顫抖的手,來(lái)看下最終效果咋樣。

是不是覺(jué)得有那么一絲絲韻味呢...

總結(jié)

 

今天派森醬帶大家一起實(shí)現(xiàn)了 Excel 像素畫(huà),小伙伴們可以發(fā)揮自己的想象,比如把女神的頭像藏進(jìn) Excel 中然后發(fā)她,你猜女神會(huì)不會(huì)被驚艷到呢。

 

責(zé)任編輯:武曉燕 來(lái)源: Python技術(shù)
相關(guān)推薦

2021-05-25 10:05:39

Python模擬導(dǎo)彈代碼

2020-12-17 10:23:41

死鎖LinuxLockdep

2025-02-28 09:30:00

?DeepSeekDeepGEMMAI

2015-07-30 09:20:26

微軟Android Lau

2022-10-19 07:35:28

2023-04-10 07:26:28

UseStateUseReducer

2020-05-28 11:00:40

Flutter代碼框架

2019-02-12 11:07:49

2020-04-09 09:52:42

Python數(shù)據(jù)技術(shù)

2021-08-03 22:26:46

Go函數(shù)分頁(yè)

2021-07-27 05:49:59

MySQL數(shù)據(jù)庫(kù)中間件

2024-03-18 09:24:12

RocketMQ消息模型分布式

2019-08-09 15:07:33

TomcatJaegerSpringBoot

2012-05-10 10:04:00

IE9IE瀏覽器

2023-05-25 10:03:40

2023-03-09 14:16:00

AIChatGPT

2022-08-25 17:47:21

PythonExcel

2021-11-12 14:17:36

AI 6G人工智能

2023-11-06 06:52:51

2023-06-05 12:41:52

研究AI
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)