Skip to content

Instantly share code, notes, and snippets.

@quxiaofeng
Created April 22, 2016 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quxiaofeng/923e0b77c834dcdfe8d5944d9ebc50e9 to your computer and use it in GitHub Desktop.
Save quxiaofeng/923e0b77c834dcdfe8d5944d9ebc50e9 to your computer and use it in GitHub Desktop.
Generate QRCode from URL. 为 URL 生成 QR 二维码,方便手机拍摄与转发。
import qrcode
from PIL import ImageFont, ImageDraw
def generate_qrcode_from_url(
info = 'http://www.quxiaofeng.me/about',
text_position = (43,10),
code_version = 1,
code_error_correction = qrcode.constants.ERROR_CORRECT_H,
code_box_size = 10,
code_border = 4,
font_file_name = 'inconsolata.ttf',
font_size = 18,
):
"""
Generate QRCode from a URL.
Usage (in jupyter):
from generate_qrcode_from_url import *
info = 'http://www.quxiaofeng.me/about'
img = generate_qrcode_from_url(info)
img
"""
qr = qrcode.QRCode(
version = code_version,
error_correction = code_error_correction,
box_size = code_box_size,
border = code_border,
)
qr.add_data(info)
qr.make(fit=True)
img = qr.make_image()
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(font_file_name, font_size)
draw.text(text_position, info, font=font)
return img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment