1. 安装

pip install qrcode

安装Image包

pip install Image

1.1 在代码中使用

import qrcode

img = qrcode.make('输入一个网址')
# img <qrcode.image.pil.PilImage object at 0x1044ed9d0> with open('test.png', 'wb') as f:
img.save(f)

这样会生成一个带有网址的二维码,文件会保存到硬盘中

1.2 在python中调用

import qrcode
from qrcode.image.pure import PymagingImage
img = qrcode.make('Some data here', image_factory=PymagingImage)一个

2. 在django中使用

  2.1 新建一个项目,tools应用

django-admin.py startproject xiangmu
python manage.py startapp tools

  2.2 将tools应用添加到项目配置文件中

INSTALLED_APPS = (

    ...

    'tools',
)

  2.3 修改tools下的视图

from django.http import HttpResponse
import qrcode
from django.utils.six import BytesIO def generate_qrcode(request, data):
img = qrcode.make(data) buf = BytesIO()
img.save(buf)
image_stream = buf.getvalue() response = HttpResponse(image_stream, content_type="image/png")
return response

  2.4 添加视图函数在项目/urls中 

  url(r'^qrcode/(.+)$', 'tools.views.generate_qrcode', name='qrcode'),

  2.5 同步数据库,打开开发服务器

python manage.py syncdb
python manage.py runserver

最新文章

  1. 自定义 Material Design风格的提示框
  2. (ORA-12899) 10g数据库导入到11g数据库时报错
  3. httpsClient抓取证书
  4. spart快速大数据分析学习提纲(一)
  5. 轻量级的中文分词工具包 - IK Analyzer
  6. if简单的写法之范围
  7. python自动发邮件
  8. 新概念英语(1-119)who call out to the thieves in the dark?
  9. Net Core Docker 容器部署,修改,保存
  10. C++11 vector使用emplace_back代替push_back
  11. JxBrowser之五:清除cache和cookie以及SSL证书处理
  12. spring-boot-2.0.3应用篇 - shiro集成
  13. java Concurrent包学习笔记(七):ConcurrentHashMap
  14. hud 3123 GCC
  15. 新概念 Lesson 3 Nice to meet you
  16. Maven解读:强大的依赖体系
  17. 实现asp.net的文件压缩、解压、下载
  18. 《DSP using MATLAB》 Problem 2.3
  19. 峰Spring4学习(7)spring对JDBC的支持
  20. 优化技术之Android UI优化

热门文章

  1. sublime text3 注册码
  2. 键值编码 KVC
  3. C++11 多线程相关的头文件
  4. Educational Codeforces Round 18 B
  5. 线段树(单点更新) HDU 1754 I Hate It
  6. linux的目录结构详细介绍
  7. 利用uiautomator实现Android移动app启动时间的测试
  8. (025)[系统故障]XP下禁止将串口设备识别成鼠标(转)
  9. 在虚拟机里安装windows或Linux系统时,安装窗口过大按钮有时点不到解决办法(图文详解)
  10. Design Patterns Uncovered: The Chain Of Responsibility Pattern