Created
April 22, 2016 07:28
-
-
Save quxiaofeng/923e0b77c834dcdfe8d5944d9ebc50e9 to your computer and use it in GitHub Desktop.
Generate QRCode from URL. 为 URL 生成 QR 二维码,方便手机拍摄与转发。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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