Simple QR code generator in Python for Casper

By Pfed-prog | BlockChain Tech | 28 Nov 2021


As part of the Gitcoin bounty for Casper hackathon we developed web application in python Flask that generates QR code. We hosted the application at on AWS Elastic Beanstalk with GitHub pipeline.

The GitHub repository for the project.

The overview of the project

The app generates QR codes for transactions on Casper local nodes, casper.live and testnet.casper.live accounting for the user’s inputs. After displaying the QR Code the user can use the button to download the image for transactions on Testnet.

Code

import base64
from flask import Flask, render_template, request, send_from_directory
from PIL import Image
from io import BytesIO

import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.moduledrawers import RoundedModuleDrawer
from qrcode.image.styles.colormasks import SquareGradiantColorMask


application = Flask(__name__)
app = application


def return_image(image):
    data = BytesIO()
    image.save(data, "PNG")
    encoded_img_data = base64.b64encode(data.getvalue())
    return encoded_img_data

def generate_image(address:str='', amount=0, message:str='', transaction:str='', network:str='casper'):

    QRcode = qrcode.QRCode(
        error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=8, version=1,
    )

    # taking url or text
    if network =='casper':
        url = f"{network}:{address}?amount={amount}&message={message}&transfer_id={transaction}"
    else:
        url = f"{network}?recipient={address}&amount={amount}&transfer_id={transaction}"
    # addingg URL or text to QRcode
    QRcode.add_data(url)

    # generating QR code
    QRcode.make(fit=True)

    # get the logo and resize it
    Logo_link = './static/logo3.png'
    # taking base width
    basewidth = 100
    logo = Image.open(Logo_link)
    # adjust image size
    wpercent = (basewidth/float(logo.size[0]))
    hsize = int((float(logo.size[1])*float(wpercent)))
    logo = logo.resize((basewidth, hsize), Image.ANTIALIAS)

    # adding color to QR code
    QRimg = QRcode.make_image(
        back_color="white",\
        image_factory=StyledPilImage,\
        module_drawer=RoundedModuleDrawer(),\
        #color_mask=SquareGradiantColorMask()\
    ).convert('RGB')

    # position the logo
    pos = ((QRimg.size[0] - logo.size[0]) // 2,
            (QRimg.size[1] - logo.size[1]) // 2)
    QRimg.paste(logo,pos)# put QRimg.paste(logo,pos,log) to make the logo fully transparent

    # set size of QR code
    return QRimg.resize((400, 400), Image.ANTIALIAS)

@app.route("/", methods=["GET","POST"])
def home():
    if request.method == "POST":
        data = [x for x in request.form.values()]
        image = generate_image(data[0], data[1], data[2], data[3], data[4])

    else: 
        image = generate_image()

    image.save('./static/output.png')
    img_data = return_image(image)
    return render_template("home.html", img_data = img_data.decode('utf-8'), mth=request.method)

@app.route("/img", methods=["GET"])
def send():
    return send_from_directory(directory='./static/',path='output.png',as_attachment=True)

if __name__ == "__main__":
    app.run(debug=True)

How do you rate this article?


12

0

Pfed-prog
Pfed-prog

web 3 Developer https://dspyt.com


BlockChain Tech
BlockChain Tech

Interesting Blockchain technology and breakthrough functionality

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.