zoukankan      html  css  js  c++  java
  • Python代码,将图片转为了Excel

    Python代码,将图片转为了Excel
    原理很简单,就是将图片每个像素的颜色填充到Excel对应的单元格中。

    from PIL import Image
    import openpyxl
    from openpyxl.styles import PatternFill, Fill
    imageFileName = 'horse.jpg' #图片文件名
    image = Image.open(imageFileName) #打开图片
    wb = openpyxl.Workbook() #创建Excel
    sheet = wb.create_sheet(imageFileName) #创建sheet
    imgW, imgH = image.size #获取图片大小
    for w in range(imgW):
        for h in range(imgH):
            #将每个像素的颜色填充到对应cell的背景色中
            rgba = image.getpixel((w,h))
            colorHex = hex(rgba[0])[2:].zfill(2) + hex(rgba[1])[2:].zfill(2) + hex(rgba[2])[2:].zfill(2)
            fill = PatternFill(fill_type = 'solid', start_color=colorHex, end_color=colorHex)
            sheet.cell(row = h + 1, column = w + 1).fill = fill
    wb.save(imageFileName + '.xlsx') #保存xlsx文件
    
  • 相关阅读:
    Rom定制
    android home键2
    蓝牙分享
    关闭系统锁屏
    android home键
    android view 背景重复
    android 找开软件所在市场页面
    jquery 选项卡
    ajaxfileupload ie 多参数
    找回 ie 图标
  • 原文地址:https://www.cnblogs.com/huanu/p/11372595.html
Copyright © 2011-2022 走看看