如何用Python中19行代碼把照片寫入到Excel中
1、前言
這里說(shuō)的,不是截取一張圖片,粘貼到excel;而是通過(guò)像素寫入到excel中,下面來(lái)看看具體的實(shí)現(xiàn)過(guò)程吧
2、代碼實(shí)戰(zhàn)
2.1 思路
我們今天要分享的,是把圖片寫入到excel中,
我們來(lái)捋一下思路:
- 準(zhǔn)備源圖片,目標(biāo)
excel
; - 通過(guò)
Pillow
來(lái)讀圖片的取像素(RGB); - 通過(guò)
openpyxl
向excel cell
內(nèi)填充十六進(jìn)制色值; - 最后把轉(zhuǎn)換的RGB像素直接寫入到
excel
中; - 說(shuō)到這里,就們就來(lái)分步實(shí)現(xiàn)。
2.2 文件準(zhǔn)備
需要寫入而Excel的源圖片:
2.3 實(shí)戰(zhàn)
安裝:
本次需要用到兩個(gè)模塊: Pillow
和 openpyxl
。
老規(guī)矩, pip 方式安裝:
pip install Pillow? pip install openpyxl?
2.4 色值轉(zhuǎn)換
由于 圖片讀取的像素是RGB值,而excel
是十六進(jìn)制寫入,
所以需要定義一個(gè)RGB與十六進(jìn)制轉(zhuǎn)換的函數(shù)
''' 定義RGB 和十六進(jìn)制色值轉(zhuǎn)換函數(shù) ''' def rgb_to_hex(rgb): ? ? rgb = rgb.split(',') ? ? color = '' ? ? #循環(huán)遍歷 ? ? for i in rgb: ? ? ? ? num = int(i) ? ? ? ? color ?+= str(hex(num))[-2:].replace('x','0').upper() ? ? return ?color
2.5 圖片轉(zhuǎn)換
此操作是逐行讀取圖片的 RGB
色值,再將 RGB 色值轉(zhuǎn)換為十六進(jìn)制色值填充到 Excel 的 cell 中。
逐行讀取圖片中的RGB色值,再將RGB色值轉(zhuǎn)換十六進(jìn)制,填充到excel中:
def img_to_excel(img_path,excel_path): ? ? #讀取源圖片 ? ? img_src = Image.open(img_path) ? ? #設(shè)置圖片寬高 ? ? img_width = img_src.size[0] ? ? img_hight = img_src.size[1] ? ? #圖片加載 ? ? str_strlist = img_src.load() ? ? #獲取當(dāng)前的excel文件 ? ? wb = openpyxl.Workbook() ? ? #保存文件 ? ? wb.save(excel_path) ? ? #打開(kāi)excel_path 下的excel文件,并寫入信息 ? ? wb = openpyxl.load_workbook(excel_path) ? ? cell_width,cell_height = 1.0,1.0 ? ? #設(shè)置excel的寫入頁(yè) ? ? sheet = wb['Sheet'] ? ? #循環(huán)圖片的高與寬,并存入 ? ? for w in range(img_width): ? ? ? ? for h in range(img_hight): ? ? ? ? ? ? data = str_strlist[w,h] ? ? ? ? ? ? color = str(data).replace("(","").replace(")","") ? ? ? ? ? ? color ?= rgb_to_hex(color) ? ? ? ? ? ? #設(shè)置填充顏色為color ? ? ? ? ? ? fille = PatternFill("solid",fgColor = color) ? ? ? ? ? ? sheet.cell(h + 1,w + 1).fill = fille ? ? #循環(huán)遍歷row,讓其全部寫入 ? ? for i in range(1,sheet.max_row + 1): ? ? ? ? sheet.row_dimensions[i].height = cell_height ? ? #循環(huán)遍歷column,讓其全部寫入 ? ? for i in range(1,sheet.max_column + 1): ? ? ? ? sheet.column_dimensions[get_column_letter(i)].width = cell_width ? ? #保存文件 ? ? wb.save(excel_path) ? ? #關(guān)閉 ? ? img_src.close()
2.6 代碼整合
import ?openpyxl from openpyxl.styles import PatternFill from openpyxl.utils import ?get_column_letter from PIL import Image,ImageFont,ImageDraw,ImageColor ''' 色值轉(zhuǎn)換: 從圖片讀取的像素塊色值是 RGB 值, RGB 和十六進(jìn)制色值轉(zhuǎn)換。 ''' def rgb_to_hex(rgb): ? ? rgb = rgb.split(',') ? ? color = '' ? ? #循環(huán)遍歷 ? ? for i in rgb: ? ? ? ? num = int(i) ? ? ? ? color ?+= str(hex(num))[-2:].replace('x','0').upper() ? ? return ?color ''' 圖片轉(zhuǎn)換: 逐行讀取圖片中的RGB色值,再將RGB色值轉(zhuǎn)換十六進(jìn)制,填充到excel中 ''' def img_to_excel(img_path,excel_path): ? ? #讀取源圖片 ? ? img_src = Image.open(img_path) ? ? #設(shè)置圖片寬高 ? ? img_width = img_src.size[0] ? ? img_hight = img_src.size[1] ? ? #圖片加載 ? ? str_strlist = img_src.load() ? ? #獲取當(dāng)前的excel文件 ? ? wb = openpyxl.Workbook() ? ? #保存文件 ? ? wb.save(excel_path) ? ? #打開(kāi)excel_path 下的excel文件,并寫入信息 ? ? wb = openpyxl.load_workbook(excel_path) ? ? cell_width,cell_height = 1.0,1.0 ? ? #設(shè)置excel的寫入頁(yè) ? ? sheet = wb['Sheet'] ? ? #循環(huán)圖片的高與寬,并存入 ? ? for w in range(img_width): ? ? ? ? for h in range(img_hight): ? ? ? ? ? ? data = str_strlist[w,h] ? ? ? ? ? ? color = str(data).replace("(","").replace(")","") ? ? ? ? ? ? color ?= rgb_to_hex(color) ? ? ? ? ? ? #設(shè)置填充顏色為color ? ? ? ? ? ? fille = PatternFill("solid",fgColor = color) ? ? ? ? ? ? sheet.cell(h + 1,w + 1).fill = fille ? ? #循環(huán)遍歷row,讓其全部寫入 ? ? for i in range(1,sheet.max_row + 1): ? ? ? ? sheet.row_dimensions[i].height = cell_height ? ? #循環(huán)遍歷column,讓其全部寫入 ? ? for i in range(1,sheet.max_column + 1): ? ? ? ? sheet.column_dimensions[get_column_letter(i)].width = cell_width ? ? #保存文件 ? ? wb.save(excel_path) ? ? #關(guān)閉 ? ? img_src.close() if __name__ == '__main__': ? ? #源圖片地址 ? ? img_path = './queue.jgp' ? ? #保存excel地址 ? ? excel_path = './queue.xlsx' ? ? #執(zhí)行 ? ? img_to_excel(img_path, excel_path)
2.7 運(yùn)行結(jié)果
3、總結(jié)
這里提醒一下:
如果你的源圖片很大,運(yùn)行完成后,打開(kāi)Excel會(huì)提示文件損壞,
因?yàn)镋xcel的行數(shù)有限,導(dǎo)致無(wú)法全部寫完數(shù)據(jù)。
Excel報(bào)錯(cuò)詳情:
xml報(bào)錯(cuò)詳情:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><logFileName>error254800_05.xml</logFileName><summary>在文件“D:\Project\img\king.xlsx”中檢測(cè)到錯(cuò)誤</summary><removedParts><removedPart>已刪除的部件: 部件 /xl/styles.xml。 (樣式)</removedPart></removedParts><repairedRecords><repairedRecord>已修復(fù)的記錄: /xl/worksheets/sheet1.xml 部分的 單元格信息</repairedRecord></repairedRecords></recoveryLog>
到此這篇關(guān)于如何用Python中19行代碼把照片寫入到Excel中的文章就介紹到這了,更多相關(guān)Python3把照片寫入到Excel中內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python 3.x讀寫csv文件中數(shù)字的方法示例
在我們?nèi)粘i_(kāi)發(fā)中經(jīng)常需要對(duì)csv文件進(jìn)行讀寫,下面這篇文章主要給大家介紹了關(guān)于Python 3.x讀寫csv文件中數(shù)字的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08Django?url.py?path?name同一app下路由別名定義
這篇文章主要為大家介紹了Django?url.py?path?name同一app下路由別名定義詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Python 字符串操作實(shí)現(xiàn)代碼(截取/替換/查找/分割)
這篇文章主要介紹了Python 字符串截取/替換/查找/分割等實(shí)現(xiàn)方法,需要的朋友可以參考下2013-06-06python實(shí)現(xiàn)將pvr格式轉(zhuǎn)換成pvr.ccz的方法
這篇文章主要介紹了python實(shí)現(xiàn)將pvr格式轉(zhuǎn)換成pvr.ccz的方法,涉及Python實(shí)現(xiàn)格式轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04詳解Python3.6安裝psutil模塊和功能簡(jiǎn)介
這篇文章主要介紹了詳解Python3.6安裝psutil模塊和功能簡(jiǎn)介,詳細(xì)的介紹了安裝psutil模塊和該模塊的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Python語(yǔ)法糖遍歷列表時(shí)刪除元素方法示例詳解
這篇文章主要為大家介紹了Python語(yǔ)法糖遍歷列表時(shí)刪除元素詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Python使用random模塊實(shí)現(xiàn)擲骰子游戲的示例代碼
這篇文章主要介紹了Python使用random模塊實(shí)現(xiàn)擲骰子游戲的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04