Skip to content

Commit

Permalink
Add script to generate QR codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Max-7 committed May 27, 2021
1 parent 999ab88 commit ec8855b
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
venv
Binary file added Roboto-Bold.ttf
Binary file not shown.
51 changes: 51 additions & 0 deletions generate_qrcode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pathlib
import argparse
from PIL import Image, ImageDraw, ImageFont

import qrcode


MENU_TYPES = {
"Drinks": ("Boissons", 310, 90),
"Snacks": ("Snacks", 250, 90),
}


def generate_text_image(text, width, height):
img = Image.new("RGB", (width, height), color = (255, 255, 255))
d = ImageDraw.Draw(img)
font = ImageFont.truetype("Roboto-Bold.ttf", 70)
d.text((10, 5), text, fill=(0, 0, 0), font=font)
return img


def generate_qr_code(uri, text):
qr_code = qrcode.QRCode(version=None, error_correction=qrcode.constants.ERROR_CORRECT_M, border=4, box_size=20)
qr_code.add_data(uri)
qr_code.make()
qr_img = qr_code.make_image()
height = 40 if text else 0
final_img = Image.new("RGB", (qr_img.size[0], qr_img.size[1] + height), (255, 255, 255))
final_img.paste(qr_img, (0, height))

if text:
text_img = generate_text_image(*MENU_TYPES[text])
x = qr_img.size[0] / 2 - text_img.size[0] / 2
final_img.paste(text_img, (int(x), 10))

return final_img


if __name__ == "__main__":
parser = argparse.ArgumentParser()

parser.add_argument("uri")
parser.add_argument("-o", "--output", type=pathlib.Path, default="qrcode.png")
parser.add_argument("-t", "--text", type=str, choices=["Snacks", "Drinks", None], required=True)

args = parser.parse_args()
img = generate_qr_code(args.uri, args.text)
img.save(args.output)

img = generate_text_image(*MENU_TYPES["Snacks"])
img.save("a.png")
Binary file added qr_drinks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added qr_snacks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Pillow==8.2.0
qrcode==6.1
six==1.16.0
2 changes: 2 additions & 0 deletions urls.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://github.com/Max-7/marquise/blob/master/drinks.pdf?raw=true
https://github.com/Max-7/marquise/blob/master/snacks.png?raw=true

0 comments on commit ec8855b

Please sign in to comment.