[Note] How to write QR code and description in the same image with python

The finished product

qr.png

Preparation

Install what you need to run on colab.

Installation of required libraries


pip install qrcode pillow

Install the font to write the Japanese character string to the image with the prepared sample


!apt-get -y install fonts-ipafont-gothic

Script real part


import qrcode
import PIL.Image
import PIL.ImageDraw
import PIL.ImageFont

imgpath  = "qr.png " #Image storage location and file name
text     = "Return" #Information you want to incorporate into the QR code
#This time I want to add Japanese notation, so I prepared a Japanese font
fontname = "/usr/share/fonts/opentype/ipafont-gothic/ipag.ttf"
#Character size to be written
fontsize = 36

#Function to get the font size to be written
def getTextSize(_text="test", _fontname="", _fontsize=36):
  canvasSize    = (1, 1)
  backgroundRGB = (255, 255, 255)
  img  = PIL.Image.new("RGB", canvasSize, backgroundRGB)
  draw = PIL.ImageDraw.Draw(img)
  font = PIL.ImageFont.truetype(_fontname, _fontsize)
  textWidth, textHeight = draw.textsize(_text, font=font)
  return textWidth, textHeight

#Create a QR code and add the character notation to the bottom center
def makeQRimg(_imgpath, _text, _fontname="", _fontsize=36):
  #Maximum error correction level, margin is the minimum width in the specifications
  qr = qrcode.QRCode(
      error_correction=qrcode.constants.ERROR_CORRECT_H,
      border=4,
  )
  qr.add_data(_text)
  qr.make()
  img = qr.make_image().convert("RGB")

  textSize    = getTextSize(_text, _fontname, _fontsize)
  textTopLeft = (img.size[0]/2 - textSize[0]/2, img.size[1] - textSize[1])
  draw = PIL.ImageDraw.Draw(img)
  font = PIL.ImageFont.truetype(_fontname, _fontsize)
  draw.text(xy=textTopLeft, text=_text, fill=(0, 0, 0), font=font)

  #Save QR code
  img.save(_imgpath)

#Run
makeQRimg(imgpath, text, fontname, fontsize)
#Display the created QR code on IPython
from IPython.display import Image, display_png
display_png(Image(imgpath))

Recommended Posts

[Note] How to write QR code and description in the same image with python
How to display bytes in the same way in Java and Python
How to write the correct shebang in Perl, Python and Ruby scripts
How to get the date and time difference in seconds with python
Convert the image in .zip to PDF with Python
How to get a list of files in the same directory with python
How to generate a QR code and barcode in Python and read it normally or in real time with OpenCV
How to log in to AtCoder with Python and submit automatically
Recursively get the Excel list in a specific folder with python and write it to Excel.
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
The 15th offline real-time how to write reference problem in Python
I wrote the code to write the code of Brainf * ck in python
Difference in how to write if statement between ruby ​​and python
The 14th offline real-time how to write reference problem in python
How to build Python and Jupyter execution environment with VS Code
The 18th offline real-time how to write reference problem in Python
How to work with BigQuery in Python
How to adjust image contrast in Python
How to use is and == in Python
How to write Ruby to_s in Python
The 16th offline real-time how to write problem was solved with Python
The 17th offline real-time how to write reference problem implemented in Python
How to crop the lower right part of the image with Python OpenCV
The 16th offline real-time how to write reference problem to solve with Python
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
[Introduction to Python] How to write a character string with the format function
The 19th offline real-time how to write reference problem to solve with Python
The 15th offline real-time how to write problem was solved with python
How to use the C library in Python
[REAPER] How to play with Reascript in Python
How to generate permutations in Python and C ++
How to use Python Image Library in python3 series
How to crop an image with Python + OpenCV
Detect folders with the same image in ImageHash
The trick to write flatten concisely in python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
How to write async and await in Vue.js
How to plot autocorrelation and partial autocorrelation in python
How to get started with the 2020 Python project (windows wsl and mac standardization)
How to pass the path to the library built with pyenv and virtualenv in PyCharm
How to display in the entire window when setting the background image with tkinter
Note: How to get the last day of the month with python (added the first day of the month)
QR code creation with Python. I don't want to line up because I write the same thing every time I issue a commuter pass. ..
(Diary 1) How to create, reference, and register data in the SQL database of Microsoft Azure service with python
I want to write in Python! (1) Code format check
20th Offline Real-time How to Write Problems in Python
How to retrieve the nth largest value in Python
How to convert / restore a string with [] in python
How to get the variable name itself in python
How to get the number of digits in Python
How to write string concatenation in multiple lines in Python
How to scrape image data from flickr with python
How to do hash calculation with salt in Python
Note assigning image textures to materials in Maya python
[Introduction to Python] How to iterate with the range function?
How to know the current directory in Python in Blender
[Python] Get the numbers in the graph image with OCR
How to run tests in bulk with Python unittest
[Python] How to specify the download location with youtube-dl
Write letters in the card illustration with OpenCV python