-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
59 lines (45 loc) · 1.55 KB
/
main.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
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
from openpyxl.styles import PatternFill
from openpyxl.cell import cell
from PIL import Image
def Resize(img):
try:
img = Image.open(img)
new_img = img.resize((64,64), Image.ANTIALIAS)
size = new_img.size
if size[0] > size[1]:
maxDimensionPixel = size[0]
else:
maxDimensionPixel = size[0]
return maxDimensionPixel, new_img
except:
raise Exception("File not exist")
def excel(pixel,img):
try:
wb = Workbook()
ws = wb.active
for i in range(1,pixel-1):
col_i = get_column_letter(i)
ws.column_dimensions[col_i].width = 1
ws.row_dimensions[i].height = 4
for i in range(1,pixel-1):
for k in range(1,pixel-1):
r, g, b = img.getpixel((i,k))
hexColor = "{0:02X}{1:02X}{2:02X}".format(r, g, b)
myCell = ws.cell(row=k,column=i)
ws[myCell.coordinate].fill = PatternFill(fgColor=hexColor, fill_type = "solid")
wb.save("output.xlsx")
return True
except Exception as e:
print(e)
return False
if __name__ == '__main__':
print("Welcome to Image To Excel Python Program\nType a file name (E.g input.jpeg ) \nIt's have to exist same this python's script directory")
img = input("File name: ")
pixel, img = Resize(img)
status = excel(pixel,img)
if status == True:
print("Success!")
else:
print("Fail :(")