小屌丝:鱼哥,新年快乐~ ~
小鱼:新年快乐,小屌丝,虽然是元旦,但是也算是迈入新的一年了,也该有新的形象了。
小屌丝:鱼哥,你说的没错啊,新的一年,我要用新的姿势去追妹子。
小鱼:… 怎么三句不说妹子,你无话可说了??
小屌丝:妹子是我提升技术的动力!
小鱼:… 好吧,只要你能提升技能,其他的都无所谓…
小屌丝:呦吼~~ 对了鱼哥,我记得你有一篇博文,把文字写在图片里。
小鱼:对哦,不少小伙伴通过这个方式,让自己的女友感动了。
小屌丝:那能不能把图片写在别的地方呢?
小鱼:例如呢??
小屌丝:excel,word,PDF 等等…
小鱼:…唉我去~~
小屌丝:嘿嘿~ ~ 来嘛 ~ ~
我们今天要分享的,是把图片写入到excel中,
注意:
这里说的,不是截取一张图片,粘贴到excel;而是通过像素写入到excel中。
我们来捋一下思路:
说到这里,就们就来分步实现。
需要写入而Excel的源图片:
本次需要用到两个模块: Pillow 和 openpyxl 。
老规矩, pip 方式安装:
- pip install Pillow
- pip install openpyxl
-
其他方式安装:
《Python3,选择Python自动安装第三方库,从此跟pip说拜拜!!》
《Python3:我低调的只用一行代码,就导入Python所有库!!》
1、色值转换
由于 图片读取的像素是RGB值,而excel是十六进制写入,
所以需要定义一个RGB与十六进制转换的函数
- # -*- coding:utf-8 -*-
- # @Time : 2022-01-03
- # @Author : carl_DJ
-
- '''
- 定义RGB 和十六进制色值转换函数
- '''
-
- def rgb_to_hex(rgb):
- rgb = rgb.split(',')
- color = ''
- #循环遍历
- for i in rgb:
- num = int(i)
- color += str(hex(num))[-2:].replace('x','0').upper()
- return color
-
-
2、图片转换
此操作是逐行读取图片的 RGB 色值,再将 RGB 色值转换为十六进制色值填充到 Excel 的 cell 中。
- # -*- coding:utf-8 -*-
- # @Time : 2022-01-03
- # @Author : carl_DJ
-
-
- '''
- 逐行读取图片中的RGB色值,再将RGB色值转换十六进制,填充到excel中
- '''
-
- def img_to_excel(img_path,excel_path):
- #读取源图片
- img_src = Image.open(img_path)
- #设置图片宽高
- img_width = img_src.size[0]
- img_hight = img_src.size[1]
-
- #图片加载
- str_strlist = img_src.load()
- #获取当前的excel文件
- wb = openpyxl.Workbook()
- #保存文件
- wb.save(excel_path)
- #打开excel_path 下的excel文件,并写入信息
- wb = openpyxl.load_workbook(excel_path)
- cell_width,cell_height = 1.0,1.0
-
- #设置excel的写入页
- sheet = wb['Sheet']
-
- #循环图片的高与宽,并存入
- for w in range(img_width):
- for h in range(img_hight):
- data = str_strlist[w,h]
- color = str(data).replace("(","").replace(")","")
- color = rgb_to_hex(color)
-
- #设置填充颜色为color
- fille = PatternFill("solid",fgColor = color)
- sheet.cell(h + 1,w + 1).fill = fille
-
- #循环遍历row,让其全部写入
- for i in range(1,sheet.max_row + 1):
- sheet.row_dimensions[i].height = cell_height
- #循环遍历column,让其全部写入
- for i in range(1,sheet.max_column + 1):
- sheet.column_dimensions[get_column_letter(i)].width = cell_width
-
- #保存文件
- wb.save(excel_path)
- #关闭
- img_src.close()
-
-
3、代码整合
- # -*- coding:utf-8 -*-
- # @Time : 2022-01-03
- # @Author : carl_DJ
-
- import openpyxl
- from openpyxl.styles import PatternFill
- from openpyxl.utils import get_column_letter
- from PIL import Image,ImageFont,ImageDraw,ImageColor
-
-
- '''
- 色值转换:
- 从图片读取的像素块色值是 RGB 值,
- RGB 和十六进制色值转换。
- '''
-
- def rgb_to_hex(rgb):
- rgb = rgb.split(',')
- color = ''
- #循环遍历
- for i in rgb:
- num = int(i)
- color += str(hex(num))[-2:].replace('x','0').upper()
- return color
-
-
- '''
- 图片转换:
- 逐行读取图片中的RGB色值,再将RGB色值转换十六进制,填充到excel中
- '''
-
- def img_to_excel(img_path,excel_path):
- #读取源图片
- img_src = Image.open(img_path)
- #设置图片宽高
- img_width = img_src.size[0]
- img_hight = img_src.size[1]
-
- #图片加载
- str_strlist = img_src.load()
- #获取当前的excel文件
- wb = openpyxl.Workbook()
- #保存文件
- wb.save(excel_path)
- #打开excel_path 下的excel文件,并写入信息
- wb = openpyxl.load_workbook(excel_path)
- cell_width,cell_height = 1.0,1.0
-
- #设置excel的写入页
- sheet = wb['Sheet']
-
- #循环图片的高与宽,并存入
- for w in range(img_width):
- for h in range(img_hight):
- data = str_strlist[w,h]
- color = str(data).replace("(","").replace(")","")
- color = rgb_to_hex(color)
-
- #设置填充颜色为color
- fille = PatternFill("solid",fgColor = color)
- sheet.cell(h + 1,w + 1).fill = fille
-
- #循环遍历row,让其全部写入
- for i in range(1,sheet.max_row + 1):
- sheet.row_dimensions[i].height = cell_height
- #循环遍历column,让其全部写入
- for i in range(1,sheet.max_column + 1):
- sheet.column_dimensions[get_column_letter(i)].width = cell_width
-
- #保存文件
- wb.save(excel_path)
- #关闭
- img_src.close()
-
-
- if __name__ == '__main__':
- #源图片地址
- img_path = './queue.jgp'
- #保存excel地址
- excel_path = './queue.xlsx'
- #执行
- img_to_excel(img_path, excel_path)
-
-
4、运行结果
写到这里,今天的分享就差不多结束了。
因为今天也用到了Pillow,如果想快速入手,可以参照小鱼的这篇博文《Python3,10行代码,我把情书写在她的照片里,她被我的才华征服了。》
这里提醒一下,如果你的源图片很大,运行完成后,打开Excel会提示文件损坏,因为Excel的行数有限,导致无法全部写完数据。
Excel报错详情:
xml报错详情:
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><logFileName>error254800_05.xml</logFileName><summary>在文件“D:\Project\img\king.xlsx”中检测到错误</summary><removedParts><removedPart>已删除的部件: 部件 /xl/styles.xml。 (样式)</removedPart></removedParts><repairedRecords><repairedRecord>已修复的记录: /xl/worksheets/sheet1.xml 部分的 单元格信息</repairedRecord></repairedRecords></recoveryLog>
-