-
Notifications
You must be signed in to change notification settings - Fork 4
/
binary_image.py
40 lines (31 loc) · 987 Bytes
/
binary_image.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
# Use this file when the image to be displayed is binary
# i.e. is only black and white, eg: QR codes, Barcodes etc
# This implementation is faster than the colored image one
# Make sure that the image is *NOT* more than 250*250 Pixels
import cv2
# Provide your filename here
file_name = 'qr.png'
img = cv2.imread(file_name,0)
RES = img.shape[0]
file_name = file_name.split('.')[0]
string = ''
# Compression and Encoding Grayscale image
map = {}
for i, row in enumerate(img):
start = []
end = []
for j, _ in enumerate(row):
# First Black Pixel Capture
if j<RES-1 and row[j] > 0 and row[j+1] == 0:
start.append(j+1)
# Last Black Pixel Capture
elif j<RES-1 and row[j] == 0 and row[j+1] > 0:
end.append(j)
if start or end:
map[i] = list(zip(start,end))
string += str(i) + ':'
string += ','.join([str(item) for sublist in map[i] for item in sublist])
string += '\n'
f = open(file_name, "w")
f.write(string)
f.close()