Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_alignment_dark():
qr = encoder.encode('A', version=12)
res = []
for row in utils.matrix_iter_verbose(qr.matrix, qr.version, border=0):
res.append(bytearray([(0x2, 0x1)[v == consts.TYPE_ALIGNMENT_PATTERN_DARK] for v in row]))
expected = read_matrix('v12-alignment-dark')
assert expected == res
def test_timing_dark_mqr():
qr = encoder.encode('A', micro=True)
res = []
for row in utils.matrix_iter_verbose(qr.matrix, qr.version, border=0):
res.append(bytearray([(0x2, 0x1)[v == consts.TYPE_TIMING_DARK] for v in row]))
expected = read_matrix('m2-timing-dark')
assert expected == res
def test_separator_mqr():
qr = encoder.encode('A', micro=True)
res = []
for row in utils.matrix_iter_verbose(qr.matrix, qr.version, border=0):
res.append(bytearray([(0x2, 0x0)[v == consts.TYPE_SEPARATOR] for v in row]))
expected = read_matrix('m2-separator')
assert expected == res
def test_convert_to_boolean():
qr = encoder.encode('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
error='m', mask=4, boost_error=False)
res = []
for row in utils.matrix_iter_verbose(qr.matrix, qr.version):
res.append(bytearray([bool(v >> 8) for v in row]))
expected = read_matrix('iso-fig-29')
assert expected == res
def test_alignment_light():
qr = encoder.encode('A', version=12)
res = []
for row in utils.matrix_iter_verbose(qr.matrix, qr.version, border=0):
res.append(bytearray([(0x2, 0x0)[v == consts.TYPE_ALIGNMENT_PATTERN_LIGHT] for v in row]))
expected = read_matrix('v12-alignment-light')
assert expected == res
def test_format_dark_and_light_qr():
qr = encoder.encode('A', micro=False)
res = []
for row in utils.matrix_iter_verbose(qr.matrix, qr.version, border=0):
res.append(bytearray([(0x2, 0x1)[v in (consts.TYPE_FORMAT_DARK, consts.TYPE_FORMAT_LIGHT)] for v in row]))
expected = read_matrix('v1-format-dark-and-light')
assert expected == res
def test_timing_dark_and_light_mqr():
qr = encoder.encode('A', micro=True)
res = []
for row in utils.matrix_iter_verbose(qr.matrix, qr.version, border=0):
res.append(bytearray([(0x2, 0x1)[v in (consts.TYPE_TIMING_DARK, consts.TYPE_TIMING_LIGHT)] for v in row]))
expected = read_matrix('m2-timing-dark-and-light')
assert expected == res
transparent_color = clr_val
break
palette[0] = transparent_color
for module_type, clr in colormap.items():
if clr == transparent:
colormap[module_type] = transparent_color
elif is_transparent: # Greyscale and transparent
if black in palette:
# Since black is zero, it should be the first entry
palette = [black, transparent]
png_trans_idx = palette.index(transparent)
# Keeps a mapping of iterator output -> color number
color_index = {}
if number_of_colors > 2:
# Need the more expensive matrix iterator
miter = matrix_iter_verbose(matrix, version, scale=1, border=0)
for module_type, clr in colormap.items():
color_index[module_type] = palette.index(clr)
else:
# Just two colors, use the cheap iterator which returns 0x0 or 0x1
miter = iter(matrix)
# The code to create the image requires that TYPE_QUIET_ZONE is available
color_index[qz_idx] = palette.index(colormap[qz_idx])
color_index[0] = color_index[qz_idx]
color_index[1] = palette.index(colormap[dark_idx])
miter = ((color_index[b] for b in r) for r in miter)
horizontal_border = b''
vertical_border = b''
if border > 0:
# Calculate horizontal and vertical border
qz_value = color_index[qz_idx]
horizontal_border = scanline(repeat(qz_value, width)) * border * scale
:param matrix: The matrix to serialize.
:param int version: The (Micro) QR code version
:param out: Filename or a file-like object supporting to write binary data.
:param scale: Indicates the size of a single module (default: 1 which
corresponds to 1 x 1 pixel per module).
:param int border: Integer indicating the size of the quiet zone.
If set to ``None`` (default), the recommended border size
will be used (``4`` for QR Codes, ``2`` for a Micro QR Codes).
"""
scale = int(scale)
width, height, border = _valid_width_height_and_border(version, scale, border)
if None in colormap.values():
raise ValueError('Transparency is not supported')
for mt, clr in colormap.items():
colormap[mt] = _color_to_rgb(clr)
row_iter = matrix_iter_verbose(matrix, version, scale, border)
with writable(out, 'wb') as f:
write = f.write
write('P6 # Created by {0}\n{1} {2} 255\n'
.format(CREATOR, width, height).encode('ascii'))
for row in row_iter:
write(b''.join(pack(b'>3B', *colormap[mt]) for mt in row))