From 1e2eb9313c973948183d6c2696345f7468ad9e05 Mon Sep 17 00:00:00 2001 From: David Beniamine <david.beniamine@tetras-libre.fr> Date: Sat, 5 Feb 2022 12:13:00 +0100 Subject: [PATCH] Add colors and logo --- pyVcardToQr/pyVcardToQr.py | 39 ++++++++++++++++++++++++++++++++++---- setup.py | 2 +- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/pyVcardToQr/pyVcardToQr.py b/pyVcardToQr/pyVcardToQr.py index 0ee20f1..65509e1 100644 --- a/pyVcardToQr/pyVcardToQr.py +++ b/pyVcardToQr/pyVcardToQr.py @@ -1,6 +1,7 @@ import vobject import click import qrcode +from PIL import Image def getCard(first, last, data): @@ -20,8 +21,36 @@ def getCard(first, last, data): return card -def genQr(string, output): - img = qrcode.make(string) +def genQr(data, output, logo=None, colors=('black', 'white'), logo_path=None): + if logo_path is not None: + logo = Image.open(logo_path) + # taking base width + basewidth = 100 + + # adjust image size + wpercent = (basewidth/float(logo.size[0])) + hsize = int((float(logo.size[1])*float(wpercent))) + logo = logo.resize((basewidth, hsize), Image.ANTIALIAS) + + QRcode = qrcode.QRCode() + + # adding URL or text to QRcode + QRcode.add_data(data) + + # generating QR code + QRcode.make() + + fill, back = colors + # adding color to QR code + img = QRcode.make_image(fill_color=fill, back_color=back).convert('RGB') + + if logo_path is not None: + # set size of QR code + pos = ((img.size[0] - logo.size[0]) // 2, + (img.size[1] - logo.size[1]) // 2) + img.paste(logo, pos) + + # save the QR code generated img.save(output) @@ -31,11 +60,13 @@ def genQr(string, output): @click.argument('output') @click.option('--data', nargs=3, type=click.Tuple([str, str, str]), multiple=True, help='some addition data to put in vcard ex --data FIELD TYPE VALUE, TYPE can be empty') -def main(first, last, output, data): +@click.option('--logo', type=click.Path(exists=True, readable=True), help="A logo to put in the qrcode") +@click.option('--colors', type=click.Tuple([str, str]), help="Colors for the qrcode") +def main(first, last, output, data, logo, colors): card = getCard(first, last, data) contents = card.serialize() print(contents) - genQr(contents, output) + genQr(contents, output, logo, colors) if __name__ == '__main__': diff --git a/setup.py b/setup.py index 7198332..d97299a 100644 --- a/setup.py +++ b/setup.py @@ -18,5 +18,5 @@ setup( description="Generate Vcard QR code", long_description=long_description, packages=find_packages(), - install_requires=["vobject", "click", "qrcode", "Image"], + install_requires=["vobject", "click", "qrcode", "Pillow"], ) -- GitLab