2

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.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
  • I removed the "postgresql" tag because it clearly isn't relevant here. This is a simple "web app" question. – Richard Huxton Dec 14 '21 at 07:57
  • 1
    Don't think you'll get away from having to save it as an image, just remove it after you've opened the image in your browser: https://stackoverflow.com/questions/3744573/python-code-to-open-image-in-browser – Mandera Dec 14 '21 at 07:59
  • It depends how you want to use it. You can probably modify both where you save and where you load the image to accept bytes but you have to do it in both places – mousetail Dec 14 '21 at 08:56
  • @Mandera is incorrect, it should be very possible to save the image to memory and use it from there – mousetail Dec 14 '21 at 08:57

4 Answers4

5

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

aSaffary
  • 793
  • 9
  • 22
  • Yes but from the answer it self **This assumes that your image is stored in PNG format, which is quite popular** which the author do not want to do. – Gaëtan GR Dec 14 '21 at 08:45
  • @GaëtanGR it can be changed to other formats which is mentioned in the answer. plus in order to create image encodings you don't have to actually save the image, you can use opencv to do it. – aSaffary Dec 14 '21 at 10:50
  • Yes it can be change to other format but the title says **without saving it as an image**, unless I misunderstood your answer there is an image being save for your solution to work – Gaëtan GR Dec 14 '21 at 10:56
  • @GaëtanGR again, you don't need to save an image to get a png format, check out my updated answer. – aSaffary Dec 14 '21 at 11:00
  • 1
    Correct ! I misunderstood the answer, thanks for the clarification. – Gaëtan GR Dec 14 '21 at 11:03
1

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}}
goodman
  • 11
  • 3
0

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"/>
0

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 }}" />
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129