python讀取圖片顏色值并生成excel像素畫的方法實例
像素畫:

需要用到的包:
進(jìn)度條:progressbar
pip install progressbar -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
excel:操作包openpyxl
pip install openpyxl -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
食用指南:
文件目錄:

運(yùn)行:
進(jìn)入程序img2excel_user.py 所在目錄,輸入:
python img2excel_user.py 圖片地址 excel保存地址(要加上excel名字)
例如:
python img2excel_user.py D:\myPythonProgram\img2excel\3.jpg D:\myPythonProgram\img2excel\3.xlsx
注意:
進(jìn)入二級目錄的方法:cd .\文件夾名

若圖片太大,生成的文件會打不開,所以準(zhǔn)備的圖片不能太大:

源碼:
# -*- coding: utf-8 -*-
from PIL import Image
import openpyxl
import openpyxl.styles
from openpyxl.styles import PatternFill
from openpyxl.utils import get_column_letter
from progressbar import *
def RGB_to_Hex(rgb):
"""
RGB顏色轉(zhuǎn)換成16進(jìn)制顏色
:param rgb:
:return:
"""
RGB = rgb.split(',') # 將RGB格式劃分開來
color = ''
for i in RGB:
num = int(i)
# 將R、G、B分別轉(zhuǎn)化為16進(jìn)制拼接轉(zhuǎn)換并大寫 hex() 函數(shù)用于將10進(jìn)制整數(shù)轉(zhuǎn)換成16進(jìn)制,以字符串形式表示
color += str(hex(num))[-2:].replace('x', '0').upper()
return color
def img2excel(img_path,excelout_path):
"""
圖片轉(zhuǎn)換成excel
:param img_path: 圖片地址
:param excelout_path: excel保存地址
:return:
"""
img_src = Image.open(img_path)
#寬高
img_width=img_src.size[0]
img_height=img_src.size[1]
print("圖片寬%s,高%s"%(img_width,img_height))
# 類型
# print(img_src.mode)
if img_src.mode != "RGB":
img_src = img_src.convert('RGB')
str_strlist = img_src.load()
wb=openpyxl.Workbook()
wb.save(excelout_path)
wb=openpyxl.load_workbook(excelout_path)
sheet=wb["Sheet"]
sheet.title="img2excel"
cell_width = 1.0
cell_height = cell_width * (2.2862 / 0.3612)
print("正在瘋狂生成excel,請耐心等待...")
#進(jìn)度條
widgets=['進(jìn)度:',Percentage(),'',Bar('#'),'',Timer(),' ', ETA(), ' ']
pb=ProgressBar(widgets=widgets)
for w in pb(range(img_width)):
for h in range(img_height):
data = str_strlist[w,h]
# 把元組rgb顏色變成字符串,轉(zhuǎn)換成16進(jìn)制顏色(1,2,3)-->'1,2,3'
color=str(data).replace("(","").replace(")","")
#16進(jìn)制的顏色,不帶前面#號的,要#自己拼接到color前面即可
color=RGB_to_Hex(color)
# 設(shè)置填充顏色為color,solid參數(shù)表示填充實色
fille=PatternFill("solid",fgColor=color)
sheet.cell(h+1,w+1).fill=fille
print("生成完成,正在設(shè)置單元格格式...")
for i in range(1, sheet.max_row+1):
sheet.row_dimensions[i].height=cell_height
for i in range(1, sheet.max_column+1):
sheet.column_dimensions[get_column_letter(i)].width = cell_width
print('格式設(shè)置完成,正在保存excel...')
wb.save(excelout_path)
img_src.close()
print("保存excel成功!請打開[%s]查看"%excelout_path)
if __name__=='__main__':
import sys,os
if len(sys.argv)!=3:
print("請輸入圖片地址和excel保存的地址\n"
"例如命令行輸入 python img2excel_user.py D:/result.png D:/outExcel.xlsx")
sys.exit(0)
else:
img_virify=['.jpg','.png','.gif','.bmp','.jpeg','.jpe','.jfif']
excel_virify=['.xlsx','.xlsm','.xltx','.xltm']
# 圖片地址
img_path=sys.argv[1]
# excel保存地址
excelout_path=sys.argv[2]
endName=os.path.splitext(img_path)
if endName[1] not in img_virify:
print("請選擇支持的圖片類型",img_virify)
sys.exit(0)
endName_excel=os.path.splitext(excelout_path)
if endName_excel[1] not in excel_virify:
print("excel 格式不支持,請選擇支持的格式",excel_virify)
sys.exit(0)
img2excel(r""+img_path+"",excelout_path)
運(yùn)行:

原圖:

效果圖:



總結(jié)
到此這篇關(guān)于python讀取圖片顏色值并生成excel像素畫的文章就介紹到這了,更多相關(guān)python讀取圖片顏色值生成excel像素畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Python如何在多層循環(huán)中使用break/continue
關(guān)于break/continue這兩個關(guān)鍵字在平常的使用過程中一直比較迷糊。所以本文將詳細(xì)講講Python如何在多層循環(huán)中使用break/continue,需要的可以參考一下2022-05-05
利用python獲取當(dāng)前日期前后N天或N月日期的方法示例
最近在工作中遇到一個需求,查找資料發(fā)現(xiàn)了一個很好的時間組件,所以下面這篇文章主要給大家介紹了關(guān)于利用python獲取當(dāng)前日期前后N天或N月日期的方法示例,需要的朋友們可以參考借鑒,下面來一起看看吧。2017-07-07
python實現(xiàn)從字符串中找出字符1的位置以及個數(shù)的方法
這篇文章主要介紹了python實現(xiàn)從字符串中找出字符1的位置以及個數(shù)的方法,對于Python字符串操作的學(xué)習(xí)有一定的幫助與借鑒作用,需要的朋友可以參考下2014-08-08
TensorFlow實現(xiàn)AutoEncoder自編碼器
這篇文章主要為大家詳細(xì)介紹了TensorFlow實現(xiàn)AutoEncoder自編碼器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
詳談python中subprocess shell=False與shell=True的區(qū)別
這篇文章主要介紹了詳談python中subprocess shell=False與shell=True的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04

