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.

4
  • I removed the "postgresql" tag because it clearly isn't relevant here. This is a simple "web app" question. Commented Dec 14, 2021 at 7: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: stackoverflow.com/questions/3744573/… Commented Dec 14, 2021 at 7: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 Commented Dec 14, 2021 at 8:56
  • @Mandera is incorrect, it should be very possible to save the image to memory and use it from there Commented Dec 14, 2021 at 8:57

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

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ë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.
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ëtanGR again, you don't need to save an image to get a png format, check out my updated answer.
Correct ! I misunderstood the answer, thanks for the clarification.
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}}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.