import openpyxl
from PIL import Image
from openpyxl.styles import PatternFill
import os
def color(value):
digit = list(map(str,range(10))) + list("ABCDEF")
string = ''
for i in value:
a1 = i // 16
a2 = i % 16
string += digit[a1] + digit[a2]
return string
wife = Image.open(f"{__file__[0:-len(os.path.basename(__file__))]}wife.jpg")
wb = openpyxl.Workbook()
ws = wb.active
ws.title = 'wife'
for y in range(wife.size[1]):
for x in range(wife.size[0]):
pix = wife.getpixel((x,y))
cell_colour = PatternFill(fill_type='solid', fgColor=color(pix))
ws.cell(row=y+1,column=x+1).fill = cell_colour
ws.row_dimensions[y+1].height = 10
print("正在进行%d行%d列颜色填充" % (x,y))
wb.save(f"{__file__[0:-len(os.path.basename(__file__))]}wife.xlsx")
原照片:
生成效果图: