-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertImageToBinaryFinal.py
More file actions
139 lines (107 loc) · 4.02 KB
/
ConvertImageToBinaryFinal.py
File metadata and controls
139 lines (107 loc) · 4.02 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
Author: Ruaa Abdulmajeed 101074335
This program will take an image and turn it into a bitmap that can be used for
an OLED screen.
"""
# from Cimpl import * # Carleton image editing library, uses PIL library
from tkinter import *
import tkinter.filedialog
from PIL import Image
import PIL.ImageTk
IMAGE_FILE_TYPES = [('All files', '.*'),
('BMP', '.bmp'),
('GIF', '.gif'),
('PNG', '.png'),
('TIFF', '.tif'),
('TIFF', '.tiff'),
('JPEG', '.jpg'),
('JPEG', '.jpeg')]
OLED_HEIGHT = 128
OLED_WIDTH = 32
def convertImageToBinary(filePath):
image = PIL.Image.open(filePath)
imageWidth, imageHeight = image.size
if (imageWidth % 8 != 0 or imageHeight % 8 != 0 or imageHeight > OLED_HEIGHT \
or imageWidth > OLED_WIDTH):
image = fixSize(image)
imageWidth, imageHeight = image.size
name = input("Please enter a name for the bitmap: ")
bitmap = "static const unsigned char PROGMEM " + name.upper() + "[] = \n{"
pix = image.load()
thresh = 200
for y in range(imageHeight):
count = 0
for x in range(imageWidth):
if (count == 0):
bitmap = bitmap + "B"
if (count < 8):
if (pix[x, y] < (thresh, thresh, thresh)):
bitmap = bitmap + "1"
else:
bitmap = bitmap + "0"
count = count + 1
if (count == 8):
if (y == imageHeight - 1 and x == imageWidth - 1):
bitmap = bitmap + "\n};"
else:
bitmap = bitmap + ", "
count = 0
bitmap = bitmap + "\n"
chooseCopyOrFile(bitmap, name, hideZeroes(bitmap))
def fixSize(image):
image = image.resize((64, 32), PIL.Image.ANTIALIAS)
return image
def choose_file():
root = Tk()
# Hide the top-level window. (We only want the Open dialogue box
# to appear.)
root.withdraw()
path = tkinter.filedialog.askopenfilename(filetypes=IMAGE_FILE_TYPES)
root.destroy()
return path
def chooseCopyOrFile(bitmap, name, html_bitmap):
copy = input("Would you like the bitmap to be added to your clipboard?\n(Yes/No)")
file = input("Would you like a file containing the bitmap to be created?\n(Yes/No)")
html = input("Would you like to create an html file with the zeroes hidden?\n(Yes/No)")
if (copy.lower() == "yes"):
root = Tk()
root.withdraw()
root.clipboard_clear()
root.clipboard_append(bitmap)
root.update() # now it stays on the clipboard after the window is closed
root.destroy()
if (file.lower() == "yes"):
filename = name.lower() + ".txt"
file = open(filename, "w+")
file.write(bitmap)
if (html.lower() == "yes"):
filename = name.lower() + ".html"
html = open(filename, "w+")
html.write(html_bitmap)
return 0
def hideZeroes(bitmap):
## returns html file w zeroes in white
html_bitmap = '<p><font face="courier"><strong>'
indexer = 0
# get rid of the stuff in the beginning
while bitmap[indexer] != '{':
indexer += 1
indexer = indexer+1
while indexer < len(bitmap) - 3: # -2 for the }; at the end
if bitmap[indexer] == '\n':
html_bitmap += "\n</p><p>"
indexer += 1
continue
if bitmap[indexer] in ['0', 'B', ',']:
html_bitmap += '<font color="white"/>'
while bitmap[indexer] in ['0', 'B', ',', ' ']:
html_bitmap += bitmap[indexer]
indexer += 1
html_bitmap += '</font>'
continue
html_bitmap += bitmap[indexer]
indexer += 1
html_bitmap += "</strong></font></p>"
return html_bitmap
# Main Script
convertImageToBinary(choose_file())