置顶 Carl_奕然 2022-01-04 12:20:08 10272 收藏 106
版权
Python 开发实战及代码讲解
专栏收录该内容
51 篇文章 53 订阅
订阅专栏
19 行代码, 把图片写到如 Excel.
2, 代码实战
2.1 思路
2.2 文件准备
2.3 实战
2.3.1 安装
2.3.2 代码实战
3, 总结
小屌丝: 鱼哥, 新年快乐~ ~
小鱼: 新年快乐, 小屌丝, 虽然是元旦, 但是也算是迈入新的一年了, 也该有新的形象了.
小屌丝: 鱼哥, 你说的没错啊, 新的一年, 我要用新的姿势去追妹子.
小鱼:... 怎么三句不说妹子, 你无话可说了??
小屌丝: 妹子是我提升技术的动力!
小鱼:... 好吧, 只要你能提升技能, 其他的都无所谓...
小屌丝: 呦吼~~ 对了鱼哥, 我记得你有一篇博文, 把文字写在图片里.
小鱼: 对哦, 不少小伙伴通过这个方式, 让自己的女友感动了.
小屌丝: 那能不能把图片写在别的地方呢?
小鱼: 例如呢??
小屌丝: Excel,Word,PDF 等等...
小鱼:... 唉我去~~
小屌丝: 嘿嘿~ ~ 来嘛 ~ ~
2, 代码实战
2.1 思路
我们今天要分享的, 是把图片写入到 Excel 中,
注意:
这里说的, 不是截取一张图片, 粘贴到 Excel;
而是通过像素写入到 Excel 中.
我们来捋一下思路:
准备源图片, 目标 Excel;
通过 Pillow 来读图片的取像素 (RGB);
通过 openpyxl 向 Excel cell 内填充十六进制色值;
最后把转换的 RGB 像素直接写入到 Excel 中;
说到这里, 就们就来分步实现.
2.2 文件准备
需要写入而 Excel 的源图片:
2.3 实战
2.3.1 安装
本次需要用到两个模块: Pillow 和 openpyxl .
老规矩, pip 方式安装:
- pip install Pillow
- pip install openpyxl
其他方式安装:
《Python3, 选择 Python 自动安装第三方库, 从此跟 pip 说拜拜!!》
《Python3: 我低调的只用一行代码, 就导入 Python 所有库!!》
2.3.2 代码实战
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, 运行结果
3, 总结
写到这里, 今天的分享就差不多结束了.
因为今天也用到了 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>
来源: https://blog.csdn.net/wuyoudeyuer/article/details/122299038