Skip to content

Instantly share code, notes, and snippets.

@Isweet
Created January 9, 2020 23:57
Show Gist options
  • Save Isweet/2057ebfc0c8d87b0dc7aa09515f2a7ee to your computer and use it in GitHub Desktop.
Save Isweet/2057ebfc0c8d87b0dc7aa09515f2a7ee to your computer and use it in GitHub Desktop.
Generate QR code and associated URL together in single image.
import sys
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import qrcode
def concat_vertical(image1, image2):
w1, h1 = image1.size
w2, h2 = image2.size
w = max(w1, w2)
h = h1 + h2
ret = Image.new('RGB', (w, h))
ret.paste(image1, (0, 0))
ret.paste(image2, (0, h1))
return ret
def main():
text = "www.google.com"
qr_i = qrcode.make(text)
text_i = Image.new('RGB', qr_i.size)
draw = ImageDraw.Draw(text_i)
font = ImageFont.truetype("Arial.ttf", 16)
draw.text((0, 0), text, (255, 255, 255), font=font)
final = concat_vertical(qr_i, text_i)
final.save('test.jpg')
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment