[Python] Generate QR code in memory

When generating a QR code and returning it with API etc. or sending an email The process of saving to a file once and reading it is included. This process causes speed issues in I / O and problems in parallel processing.

With the BytesIO module, you can generate an image in-memory without having to spit it out into a file once.

environment

Requires qrcode and Pillow libraries.

pip install qrcode pillow

Generate

from io import BytesIO
import base64
import qrcode


class QRImage():

    @staticmethod
    def to_bytes(text: str) -> bytes:
        stream = BytesIO()
        img = qrcode.make(text)
        img.save(fp, "PNG")
        stream.seek(0)
        byte_img = stream.read()
        stream.close()
        return byte_img

    @classmethod
    def to_b64(cls, text: str) -> bytes:
        byte = cls.to_bytes(text)
        return base64.b64encode(byte).decode("utf-8")

if __name__ == "__main__":
    binary = QRImage.to_bytes(text="some_text")
    base64_encoded = QRImage.to_b64(text="some_text")

Just generate a binary stream with BytesIO () and do make, read and save like a file stream.

StringIO etc. also exist and can store strings. It also supports the with syntax.

from io import StringIO

def main():
    with StringIO() as fp:
        fp.write("Hello")
        print(fp.closed)     # True
        print(fp.getvalue()) # Hello
    print(fp.closed)         # False

main()

Let's say goodbye to intermediate files.

Recommended Posts

[Python] Generate QR code in memory
Generate QR code in Python
Generate rounded thumbnails in Python
Command to generate QR code
Generate U distribution in Python
Character code learned in Python
Generate 8 * 8 (64) cubes in Blender Python
Check for memory leaks in 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
Fourier series verification code written in Python
Memory leak in Python Jupyter Lab (Notebook)?
Generate AWS-S3 signed (time-limited) URLs in Python
Automatically generate Python Docstring Comment in Emacs
When looking at memory usage in Python 3
Module to generate word N-gram in Python
Generate a class from a string in Python
Generate C language from S-expressions in Python
Get the EDINET code list in Python
Notes on using code formatter in Python
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding 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
N-Gram in Python
Programming 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
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Let's create a customer database that automatically issues a QR code in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python