在B站看到一个人在excel画人像,秀的一手好技术,但是作为半个程序猿有点忍不了,所以有了这个小脚本。
主要实现功能很简单,读取图片,把像素转化成excel的表格底色,保存到excel。
难点几个:
ws.column_dimensions[_get_column_letter(x)].width = 3 / 15
ws.row_dimensions[y].height = 6 * 3 / 16
pix
,img = Image.open(picture)
pix = img.load()
print(pix[0,0])
> (122,121,201)
openpyxl
的颜色格式是CC99FF
,所有手动转下RGB的格式。fill_1 = PatternFill("solid", fgColor=_color(pix[x - 1, y - 1]))
self.ws[cell_name].fill = fill_1
import openpyxl
from openpyxl.styles import PatternFill
from PIL import Image
from openpyxl.utils.cell import _get_column_letter
from datetime import datetime
class draw_on_excel():
def __init__(self):
self.wb = openpyxl.Workbook()
pass
def init_sheet(self, sheet_name='picture', size=(100, 100)):
print('{}: Draw begin'.format(datetime.now()))
# 调整用到的列和行的宽度高度
ws = self.wb.create_sheet(sheet_name, index=0)
for x in range(1, size[1] + 1):
ws.column_dimensions[_get_column_letter(x)].width = 3 / 15
for y in range(1, size[0] + 1):
ws.row_dimensions[y].height = 6 * 3 / 16
self.ws = ws
def draw_from(self, picture=''):
self.picture = picture
# 加载图片,读取大小
img = Image.open(picture)
pix = img.load()
def _color(value):
digit = list(map(str, range(10))) + list("ABCDEF")
if isinstance(value, tuple):
string = ''
for i in value:
a1 = i // 16
a2 = i % 16
string += digit[a1] + digit[a2]
return string
elif isinstance(value, str):
a1 = digit.index(value[1]) * 16 + digit.index(value[2])
a2 = digit.index(value[3]) * 16 + digit.index(value[4])
a3 = digit.index(value[5]) * 16 + digit.index(value[6])
return (a1, a2, a3)
# 初始化excel对应的行列
self.init_sheet(picture, img.size)
# draw picture
for x in range(1, img.size[0] + 1):
for y in range(1, img.size[1] + 1):
cell_name = '{}{}'.format(_get_column_letter(x), y)
fill_1 = PatternFill("solid", fgColor=_color(pix[x - 1, y - 1]))
self.ws[cell_name].fill = fill_1
def save_to(self, out_put=''):
self.wb.save(out_put)
print('{}: picture "{}" saved in "{}".'.format(datetime.now(), self.picture, out_put))
if __name__ == '__main__':
die = draw_on_excel()
die.draw_from('20200530194555.jpg')
die.save_to('picture.xlsx')
> 2020-05-30 20:53:37.457255: Draw begin
2020-05-30 20:55:39.862170: picture "20200530194555.jpg" saved in "picture.xlsx".