Skip to content

Instantly share code, notes, and snippets.

@vxf
Created July 20, 2020 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vxf/bd6d402561823fa5b36e955d6950d320 to your computer and use it in GitHub Desktop.
Save vxf/bd6d402561823fa5b36e955d6950d320 to your computer and use it in GitHub Desktop.
"""
Requires xlsxwriter and pillow
"""
import sys
from PIL import Image, ImageOps
import xlsxwriter
from itertools import product
HEIGHT = 100
BIT_DEPTH = 4
def get_resized():
img = Image.open("me.jpg")
w, h = img.size
img = img.resize((round(w*(HEIGHT/h)), round(HEIGHT)))
img = ImageOps.posterize(img, BIT_DEPTH)
return img
workbook = xlsxwriter.Workbook('Expenses01.xlsx')
worksheet = workbook.add_worksheet()
worksheet.set_column(0, HEIGHT, width=0.5)
for i in range(HEIGHT):
worksheet.set_row(i, height=6)
img = get_resized()
# img.show()
w, h = img.size
formats = dict()
for x, y in product(range(w), range(h)):
p = img.getpixel((x, y))
if p in formats:
cell_format = formats[p]
else:
r, g, b = p
c = '#' + ''.join('{:02X}'.format(x) for x in [r, g, b])
cell_format = workbook.add_format({'bold': True, 'bg_color': c})
formats[p] = cell_format
worksheet.write(y, x, '', cell_format)
workbook.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment