Generate QR code in Python

There are too many advertisements for WEB services that create QR codes. Furthermore, it would be nice if only URLs and specific character strings could be converted into QR codes, but there are too many functions and it is troublesome.

Alright, then let's make it for ourselves. Is the trigger.

First of all, from the external module Insco. First of all, qrcode 5.2.2 https://pypi.python.org/pypi/qrcode

Next is image processing, so Pillow. https://pypi.python.org/pypi/Pillow

Now you are ready.

makeqr.py



import qrcode
from PIL import Image

img = qrcode.make("http://www.yahoo.co.jp/")
img.save('qr_code.png')

img.show()

Actually this is all, but I want to move it for myself with Ren Saba, so I will use CGI. The following are the items actually installed on Sakura Internet. Please refer to here when using an external module with Sakura.

http://qiita.com/Gen6/items/a6747c1bb432fcec337a

makeqr.cgi



#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import sys, codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
import qrcode
from PIL import Image
import cgi
from datetime import datetime

print "Content-Type: text/html\n"

print """
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
</head>
<body>
<style>
#content{
  width: 100%;
  text-align: center;
}

#topmain {
  width: 90%;
  margin: 0 auto;
  margin-top: 100px;
  text-align: center;
}

input[type="text"] {
  width: 400px;
  height: 30px;
}

input[type="submit"] {
  background: #ff9900!important;
  color: #fff;
  font-size: 15px;
  padding: 8px;
  border-radius: 4px;
  width: 200px;
}

</style>
<div id="content">
<div id="topmain">
"""

form = cgi.FieldStorage()

newQr = form["targets"].value
today = datetime.now().strftime("%Y/%m/%d %H:%M:%S")

print "<h1>" + newQr + "</h1>"
print "<p>" + today + "</p>"
print "<p><a href=\"index.html\">Back to TOP</a></p>"

img = qrcode.make(newQr)
img.save('qr_code.png')

print "<p><img src=\"qr_code.png\"></p>"

print """
</div>
</div>
</body>
</html>
"""

That's it.

Recommended Posts

Generate QR code in Python
[Python] Generate QR code in memory
Command to generate QR code
Generate U distribution in Python
Character code learned in Python
Generate 8 * 8 (64) cubes in Blender Python
Automatically format Python code in Vim
Generate Jupyter notebook ".ipynb" in Python
Write selenium test code in python
Generate QR code using Python's "qrcode"
Code tests around time in Python
Generate a first class collection in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Generate AWS-S3 signed (time-limited) URLs in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
python character code
quicksort in python
nCr in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
[Python] Algorithm-aware code
Sqlite in python
StepAIC in Python
N-gram in python
Generate a class from a string in Python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Generate C language from S-expressions in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Get the EDINET code list in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Notes on using code formatter in Python
QR code display
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python