openpyxl对于格式处理相对是比较强大的,可以研究一下,其余的数据计算还是pandas吧。代码如下:
from PIL import Image
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
from openpyxl.styles import PatternFill, Color
# 打开要处理的图像
pic = Image.open(r"E:\Job\2.jpg")
print(pic.format,pic.size,pic.mode)
img_src = pic.resize((round(pic.size[0]*0.2),round(pic.size[1]*0.2)))
selfie = img_src.load() #读取像素点
size = img_src.size
workbook = Workbook()
worksheet = workbook.active
for row in range(1,size[1]):
for col in range(1,size[0]):
cell = worksheet.cell(column=col,row=row)
point = selfie[col-1, row-1] #扫描像素点
color = "FF%02X%02X%02X" % (point[0], point[1], point[2]) #颜色计算
cell.fill = PatternFill(patternType='solid', fgColor=Color(rgb=color)) #填充
worksheet.column_dimensions[get_column_letter(col)].width = 2 # 列宽
worksheet.row_dimensions[row].height = 5 # 行高
# 保存生成的excel文件
workbook.save(filename='E:\Job\image.xlsx')