Skip to content

python3 compatible #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions pdf417.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,48 @@ def add_quiet_zone(im):
def get_img(img_path):
return Image.open(img_path)


def print_img_info(im):
info = im.info
for item in info.items():
print item[0], ': ', item[1]
print 'image mode: ', im.mode
print 'image size: ', im.size
print 'file format: ', im.format
print 'image band: ', im.getbands()
print 'image box: ', im.getbbox()
print (item[0], ': ', item[1])
print('image mode: ', im.mode)
print('image size: ', im.size)
print('file format: ', im.format)
print('image band: ', im.getbands())
print('image box: ', im.getbbox())


def each_row(im, start, end, step=1):
for y in range(start, end, step):
yield list(im.crop((2, y, im.size[0]-2, y+1)).getdata())


# not in use
def each_column(im, start, end, step=1):
for x in range(start, end, step):
yield list(im.crop((x, 0, x+1, im.size[1])).getdata())


def reformat(row_data):
return [(i[0], len(list(i[1]))) for i in itertools.groupby(row_data)]


def get_min_width(row):
return row[1][1]


def row2syms(row, mw):
return "".join([str(i[1]/mw) for i in row])
return "".join([str(int(i[1]/mw)) for i in row])


def get_cluster(sym):
return (int(sym[0])-int(sym[2])+int(sym[4])-int(sym[6])+9)%9


pdf417_flag = False


def get_codeword(syms, which):
global pdf417_flag
start = which*8-8
Expand All @@ -71,13 +79,14 @@ def get_codeword(syms, which):
return 'end'

cluster = get_cluster(sym)
k = cluster/3
k = int(cluster/3)
for j in range(0,929):
if sym == codewords_tbl[k][j]:
return (k, j)

return False


def decode_part(part):
global text_submode, text_shift

Expand Down Expand Up @@ -190,7 +199,7 @@ def decode_cw(cw):


def decode_text(cw):
H = cw/30
H = int(cw/30)
L = cw%30

return (decode_part(H), decode_part(L))
Expand All @@ -209,27 +218,35 @@ def get_cwinfo(codewords):
length = len(codewords)
z = l2%30
v = l3%30
error_level = (z - (length-1)%3)/3
error_level = int((z - (length-1)%3)/3)
num_of_rows = v+1
return dict({'error_level':error_level, 'num_of_rows':num_of_rows})
return dict({'error_level': error_level, 'num_of_rows': num_of_rows})


def filter_quitezone(row_data):
if row_data[0][0] == 255: row_data = row_data[1:]
if row_data[-1][0] == 255: row_data = row_data[0:-1]
if row_data[0][0] == 255:
row_data = row_data[1:]
if row_data[-1][0] == 255:
row_data = row_data[0:-1]
return row_data


def filter_se_pattern(cw_of_row):
return [i[1] for i in cw_of_row[1:-1]]


def filter_err(codewords_list, error_level):
return codewords_list[:-2**(error_level+1)]


def filter_row_indicator(codewords):
return [k for i in codewords for k in i[1:-1]]


def get_content(codewords):
skip = ['ll', 'ps', 'ml', 'al', 'pl', 'as']
return "".join( [str(k) for i in codewords for k in i if k not in skip] )
return "".join( [str(k) for i in codewords for k in i if k not in skip])


def _reset_global_vars():
global text_shift, text_submode, pdf417_flag
Expand Down Expand Up @@ -259,7 +276,7 @@ def pdf417_decode(img_path):

syms = row2syms(row, mw)
#print 'Syms: ', syms
end = len(syms)/8+1
end = int(len(syms)/8+1)
for i in range(1, end):
sym = get_codeword(syms, i)
cw_of_row.append(sym)
Expand Down Expand Up @@ -293,4 +310,4 @@ def pdf417_decode(img_path):

if __name__ == '__main__':
ret = pdf417_decode(sys.argv[1])
print ret
print (ret)