def test_to_str(): s = 'Märchen' assert str(pyqrcode.create(s)) s = '外来語' qr = pyqrcode.create(s) assert str(pyqrcode.create(s))
def test_kanji_enforce_binary(): data = '点' # 1. Try usual mode --> kanji qr = pyqrcode.create(data) assert 'kanji' == qr.mode # 2. Try another encoding --> binary qr = pyqrcode.create(data, mode='binary', encoding='utf-8') assert 'binary' == qr.mode
def test_unicode_utf8(): s = '\u263A' # ☺ (WHITE SMILING FACE) try: pyqrcode.create(s, encoding='latin1') raise Exception('Expected an error for \u263A and ISO-8859-1') except ValueError: pass qr = pyqrcode.create(s, encoding='utf-8') assert 'binary' == qr.mode
def loginQrcode(self, force=False): """ 获得 bigFun 登录二维码 :param force: 不验证登录状态强行重新登录 :return: 二维码 png """ if not force: if self.apiReady == True: raise BigFunAPIisLogin qrcodcContent = self._bigFunAPI.login() buff = io.BytesIO() pyqrcodeng.create(qrcodcContent, error='L', version=5).png(buff, scale=5) return buff.getvalue()
def test_txt(): qr = pyqrcode.create('test', error='H') expected = '00000000000000000000000000000\n' \ '00000000000000000000000000000\n' \ '00000000000000000000000000000\n' \ '00000000000000000000000000000\n' \ '00001111111011101011111110000\n' \ '00001000001001100010000010000\n' \ '00001011101000100010111010000\n' \ '00001011101010011010111010000\n' \ '00001011101000110010111010000\n' \ '00001000001001010010000010000\n' \ '00001111111010101011111110000\n' \ '00000000000001001000000000000\n' \ '00000010111011000100010010000\n' \ '00000001100110001011000110000\n' \ '00001111101010001111011110000\n' \ '00001111010011011001100100000\n' \ '00001100011011100001010110000\n' \ '00000000000010111000010010000\n' \ '00001111111001100000110110000\n' \ '00001000001011101010000100000\n' \ '00001011101010110010110110000\n' \ '00001011101001000011000100000\n' \ '00001011101010001101001010000\n' \ '00001000001001110111110100000\n' \ '00001111111000100111001110000\n' \ '00000000000000000000000000000\n' \ '00000000000000000000000000000\n' \ '00000000000000000000000000000\n' \ '00000000000000000000000000000\n' assert expected == qr.text()
def test_binary_provide_encoding(): data = 'Émetteur' encoding = 'iso-8859-15' qr = pyqrcodeng.create(data, encoding=encoding) assert data.encode(encoding) == qr.data assert 'binary' == qr.mode assert encoding == qr.encoding
def createQRCode( ): #defining a function and iterating through for loop to generate all the qrcodes in the excel df = pd.read_csv("qr.csv") #reading csv file for index, values in df.iterrows(): QRcode = values["Barcode"] data = f''' QRcode:{QRcode}\n ''' image = pyqrcodeng.create(data) image.png(f"{QRcode}.png", scale=5) im = Image.open(f"{QRcode}.png") #opens the image logo = Image.open('logo.png') #opens the logo box = (85, 85, 160, 160) #box are (left, upper, right, lower). im.crop(box) region = logo region = region.resize( (75, 75) ) #box[3] - box[1], box[3] - box[1])) #(image.width-logo.wigth),(image.height-logo.height) im.paste(region, box) #text in image font1 = ImageFont.truetype("arial.ttf", 20) draw = ImageDraw.Draw(im) (x, y) = (55, -3) message = "Gravton Motors" color = 'rgb(0,0,0)' draw.text((x, y), message, fill=color, font=font1) #adding qrcode num font2 = ImageFont.truetype("arial.ttf", 15) message = f"{QRcode}" (x, y) = (65, 225) color = 'rgb(0,0,0)' draw.text((x, y), message, fill=color, font=font2) im = im.convert("RGBA") im.save(f"qrcodes/{QRcode}.png")
def test_data(serializer_name, buffer_factory, to_matrix_func, data, error, quiet_zone): """\ Creates a QR code, serializes it and checks if the serialization corresponds to the initial QR code matrix. `serializer_name` Method name to serialize the QR code `buffer_factory` Callable to construct the buffer. `to_matrix_func` Function to convert the buffer back to a matrix. `data` The input to construct the QR code. `error` ECC level `quiet_zone` quiet_zone size. """ qr = pyqrcode.create(data, error=error) out = buffer_factory() meth = getattr(qr, serializer_name) meth(out, quiet_zone=quiet_zone) matrix = to_matrix_func(out, quiet_zone) assert qr.code == matrix
def test_no_line_class(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, lineclass=None) root = _parse_xml(out) path_el = _get_path(root) assert 'class' not in path_el.attrib
def test_custom_svg_class(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, svgclass='test-class') root = _parse_xml(out) assert 'class' in root.attrib assert 'test-class' == root.attrib.get('class')
def test_viewbox(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, omithw=True) root = _parse_xml(out) assert 'viewBox' in root.attrib assert 'height' not in root.attrib assert 'width' not in root.attrib
def test_custom_line_class(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, lineclass='test-class') root = _parse_xml(out) path_el = _get_path(root) assert 'class' in path_el.attrib assert 'test-class' == path_el.attrib.get('class')
def test_scale(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, scale=2) root = _parse_xml(out) path = _get_path(root) assert path is not None assert 'scale(2)' in path.attrib['transform']
def test_version_and_error_provided(): # QR Code version 1-L: Max. 25 alphanumeric chars qr = pyqrcode.create('A' * 25, version=1, error='l') assert 1 == qr.version assert 'L' == qr.error assert '1-L' == qr.designator assert 'alphanumeric' == qr.mode assert b'A' * 25 == qr.data
def test_title2(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, title='Määhhh') root = _parse_xml(out) title_el = _get_title(root) assert title_el is not None assert 'Määhhh' == title_el.text
def no__test_binary(): """\ Would be nice if this would work as well. """ data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x00\x00\x00\x00:~\x9bU\x00\x00\x00\nIDAT\x08[c\xf8\x0f\x00\x01\x01\x01\x00\x9b\xd7\x1d\r\x00\x00\x00\x00IEND\xaeB`\x82' qr = pyqrcodeng.create(data) assert 'binary' == qr.mode assert qr.encoding is None assert data == qr.data
def test_module_color(): qr = pyqrcode.create('test') out = io.BytesIO() color = '#800080' qr.svg(out, module_color=color) root = _parse_xml(out) path = _get_path(root) assert path is not None assert color == path.attrib['stroke']
def test_omit_svgns(): qr = pyqrcode.create('test') out = io.BytesIO() qr.svg(out, svgns=False) root = _parse_xml(out) path_el = _get_path(root) assert path_el is None # (since _get_path uses the SVG namespace) path_el = root.find('path') # Query w/o namespace MUST find the path assert path_el is not None
def test_scale_float(): qr = pyqrcode.create('test') out = io.BytesIO() scale = 2.13 qr.svg(out, scale=scale) root = _parse_xml(out) path = _get_path(root) assert path is not None assert 'scale({0})'.format(scale) in path.attrib['transform']
def make_code(config): mode = config.pop('mode') if mode == 'byte': mode = 'binary' version = config.pop('version') if version: version = int(version) kw = dict(mode=mode, error=config.pop('error'), version=version) return pyqrcodeng.create(' '.join(config.pop('content')), **kw)
def test_background(): qr = pyqrcode.create('test') out = io.BytesIO() color = '#800080' qr.svg(out, background=color) root = _parse_xml(out) # Background should be the first path in the doc rect = _get_path(root) assert rect is not None assert color == rect.attrib['fill']
def qrCode( URL, evt ): #A function that can be called via main.py and generates qr-codes from the links stored in the links.py file# i = 0 for x in URL: data = URL[i] i = i + 1 qr = pyqrcodeng.create(data) qr.png(evt + 'qr_' + str(i) + '.png', scale=9) print('QR-Code:', str(i))
def test_kanji_tranform_encoding(): """Test the encoding can be set to shiftjis for utf-8 encoding. """ s = 'モンティ' s = '点茗' #Characters directly from the standard #Encode the string as utf-8 *not* shiftjis utf8 = s.encode('utf-8') qr = pyqrcode.create(utf8, encoding='utf-8') assert qr.mode == 'kanji' assert qr.encoding == 'shiftjis'
def create_qrcode(header, payload): try: data = b''.join( [header[k].to_bytes(header_size[k], 'big') for k in header]) + payload except OverflowError: raise ValueError( 'The chosen file is too big to be transfered using these QR-code parameters.' ) content = b32encode(data).decode('ascii').replace('=', '%') return pyqrcodeng.create(content, error=ec_lvl, version=qr_version, mode='alphanumeric', encoding='ascii')
def test_kanji_encoding(): s = '点茗' #Characters directly from the standard #These come from a reference image passed through the debugger codewords = [128,38,207,234,168,0,236,17,236,18,75,55,241,75,140,21, 117,174,242,221,243,87,199,123,50,169] qr = pyqrcode.create(s) #Get the binary representation of the data for the code bin_words = qr.builder.buffer.getvalue() #Convert the data into integer bytes b = [int(bin_words[i:i+8], base=2) for i in range(0, len(bin_words), 8)] #See if the calculated code matches the known code assert b == codewords
def display_console_qrcode_pyqrcodeng(url): # NOTE pyqrcodeng could be used for desktop (maybe) web browser launching with locally generated SVG and/or PNG qr = pyqrcodeng.create(url) """ print(qr.text()) print('-' *65) text_scale_factor_width = 2 white_char = u'\u2588' #white_char = '#' # does not work with my phone qrcode scanner/reader #white_char = '*' # does not work with my phone qrcode scanner/reader black_char = ' ' print(qr.text().replace('1', black_char * text_scale_factor_width).replace('0', white_char * text_scale_factor_width).encode('cp437')) print('-' *65) """ qr.term() # this "prints" to stdout/tty/console (works for win32)
def test_symbol_size(): code = pyqrcode.create('Hello world') dim = 25 quiet_zone = 0 assert (dim, dim) == code.symbol_size(1, quiet_zone=quiet_zone) quiet_zone = 1 d = (dim + 2 * quiet_zone) * quiet_zone assert (d, d) == code.symbol_size(1, quiet_zone=quiet_zone) quiet_zone = 4 # (default quiet_zone) d = (dim + 2 * quiet_zone) * 1 assert (d, d) == code.symbol_size() assert (d, d) == code.symbol_size(1) d = (dim + 2 * quiet_zone) * 4 assert d, d == code.symbol_size(4) quiet_zone = 0 d = (dim + 2 * quiet_zone) * 4 assert (d, d) == code.symbol_size(4, quiet_zone=quiet_zone)
def test_xbm_with_tkinter(): """Test XBM renderer is compatible with Tkinter """ #Python 2 vs 3 try: import Tkinter as tkinter except: import tkinter code = pyqrcode.create('Test') width, height = code.symbol_size(scale=1) code_xbm = code.xbm(scale=1) top = tkinter.Tk() bitmap = tkinter.BitmapImage(data=code_xbm) assert bitmap.width() == width assert bitmap.height() == height
def test_debug(): qr = pyqrcode.create('test') out = io.BytesIO() code = qr.code # Add some errors code[0][1] = ' ' code[0][2] = ' ' qr.svg(out, lineclass=None, quiet_zone=0, debug=True) root = _parse_xml(out) path = [ path for path in root.findall('{%s}path' % _SVG_NS) if 'class' in path.attrib ] assert 1 == len(path) path = path[0] assert 'pyqrerr' == path.attrib['class'] assert 'red' == path.attrib['stroke'] assert 'M1' in path.attrib['d'] assert 'M2' in path.attrib['d']
def test_xbm(): """Test the xbm render against a known good. This test checks the *values* contained in the XBM, not the text. """ c = pyqrcode.create('Test').xbm(scale=1) #Testing number-by-number to get more useful failure message c_width, c_height, c_bits = decompose_xbm(c) e_width, e_height, e_bits = decompose_xbm(expected) #Check the there is the same width and height assert c_width == e_width assert c_height == e_height #Check that there is the same number of bits assert len(c_bits) == len(e_bits) #Check the bit values for i in range(len(e_bits)): assert c_bits[i] == e_bits[i], "Wrong value at {0}: {1} != {2}".format( i, c_bits[i], e_bits[i])