2020-11-11 识别图片,转换成excell

2020-11-11  本文已影响0人  昨天今天下雨天1
from openpyxl import workbook
from openpyxl.utils import get_column_letter
from openpyxl.styles import PatternFill, Color
from PIL import Image

# 初始化
workbook = workbook.Workbook()
worksheet = workbook.active
im = Image.open("20200930123959.jpg")
im_width = im.size[0]
im_height = im.size[1]
pix = im.load()

# 根据照片大小循环
for row in range(1, im_height):
    for col in range(1, im_width):
        cell = worksheet.cell(column=col, row=row)
        pixpoint = pix[col - 1, row - 1]
        # 涂颜色
        pixColor = "FF%02X%02X%02X" % (pixpoint[0], pixpoint[1], pixpoint[2])
        fill = PatternFill(patternType='solid', fgColor=Color(rgb=pixColor))
        cell.fill = fill
    worksheet.row_dimensions[row].height = 6

for col in range(1, im_width):
    worksheet.column_dimensions[get_column_letter(col)].width = 1
workbook.save(filename='20200930123959.xlsx')

上一篇下一篇

猜你喜欢

热点阅读