-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExcelRedraw_Image_NotOptimized.py
58 lines (53 loc) · 1.71 KB
/
ExcelRedraw_Image_NotOptimized.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from PIL import Image
from openpyxl import Workbook
from openpyxl.styles import PatternFill
from openpyxl.utils import get_column_letter
class CellData:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
def imageProcess(imgInput, cellSize):
img = imgInput.copy()
imgWidth, imgHeight = img.size
x = 0
y = 0
isNewColumn = False
for m in range(0, imgWidth, cellSize):
for n in range(0, imgHeight, cellSize):
imgBox = img.crop((m, n, m+cellSize, n+cellSize))
cellColor = getDominantColor(imgBox)
if isNewColumn == True:
x -= imgHeight/cellSize
isNewColumn = False
listOfCells.append(CellData(x, y, cellColor))
x +=1
y +=1
isNewColumn = True
def getDominantColor(imgInput):
img = imgInput.copy()
img.convert("RGB")
img.resize((1,1))
rgb = img.getpixel((0,0))
dominantColor = '{:02x}{:02x}{:02x}'.format(rgb[0], rgb[1], rgb[2]) #convert rgb to hex
return dominantColor
# Prepare everything
wbook = Workbook()
fileName = "example.jpg"
cPixel = 5 #size of a cell
# Image process, and get the cell datas
listOfCells = []
openFile = Image.open(fileName)
imageProcess(openFile, 5)
wsheet = wbook.create_sheet()
for imgCell in listOfCells:
ex_x = imgCell.x + 1
ex_y = imgCell.y + 1
if ex_x == 1:
cd = wsheet.column_dimensions[get_column_letter(ex_y)]
cd.width = 2.8
exCell = wsheet.cell(row=(ex_x), column=(ex_y))
exCell.fill = PatternFill(patternType='solid', fgColor=str(imgCell.color))
listOfCells.clear()
wbook.save("Final.xlsx")
print("Done.")