Mounir Benhalima
Mounir Benhalima

Reputation: 31

How to create a QR code in Python without saving it as an image?

I am trying to make Qr Codes using Python on a Django applicaiton using this code :

def generate_qr_code (reference):
    qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_H,
    box_size=10,
    border=4,
    )
    qr.add_data(reference)
    qr.make(fit=True)
    img = qr.make_image(fill_color="black", back_color="white").convert('RGB')
    filename = reference+".jpeg"
    img.save("C:\\qrs\\"+filename)

Now, this function is called when I click on a "Generate Qr Code" Button. My problem is that I would like the Qr Code to be displayed on a new tab on my browser instead of it being saved as an image, as I only need to print them on paper at that moment and I have no need to keep the images.

Thank you for your help.

Upvotes: 2

Views: 4994

Answers (4)

You can create and display QR code in a new tab by clicking on the link in Django Templates with the code below:

# "my_app1/views.py"

from django.shortcuts import render
import qrcode
from io import BytesIO
from base64 import b64encode

def index(request):
    return render(request, 'index.html') 

def generate_qr_code(request):
    qr_code_img = qrcode.make("https://www.google.com/")
    buffer = BytesIO()
    qr_code_img.save(buffer)
    buffer.seek(0)
    encoded_img = b64encode(buffer.read()).decode()
    qr_code_data = f'data:image/png;base64,{encoded_img}'
    return render(request, 'qr_code.html', {'qr_code_data': qr_code_data})
# "my_app1/urls.py"

from django.urls import path
from . import views

app_name = "my_app1"

urlpatterns = [
    path('', views.index, name="index"),
    path('generate_qr_code/', views.generate_qr_code, name="generate-qr-code")
]
# "core/urls.py"

from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('my_app1/', include('my_app1.urls'))
]
{% "templates/index.html" %}

<a href="{% url 'my_app1:generate-qr-code' %}" target="_blank">
  Generate Qr Code
</a>
{% "templates/qr_code.html" %}

<img src="{{ qr_code_data }}" />

Upvotes: 0

goodman
goodman

Reputation: 21

You can use SVG format import qrcode import qrcode.image.svg from io import BytesIO

def generate_qr_code (reference):
  factory = qrcode.image.svg.SvgImage
  qr_string = "sample text"
  img = qrcode.make(qr_string, image_factory=factory, box_size=10)
  stream = BytesIO()
  img.save(stream)
  
  context = {
   'qrcode': stream.getvalue().decode()
  }

  return render(request, 'YOUR HTML.html', context)

and then you can use it in html file:

{{qrcode|safe}}

Upvotes: 1

Mounir Benhalima
Mounir Benhalima

Reputation: 31

After all, I managed to do so by using this simple line in my HTML:

<img id='barcode' src="https://api.qrserver.com/v1/create-qr-code/?data={{ref}}" alt="" title="{{ref}}" width="150" height="150"/>

Upvotes: 0

aSaffary
aSaffary

Reputation: 863

convert the image to base64 and show it in your html like this

import base64
b64 = base64.b64encode(image).decode("utf-8")

update:

ofc you don't need to save your image as png to use this feature, you can change the format in html and you can also change the image format without saving it to a file like this

Upvotes: 5

Related Questions