1

I wrote a code that creating a QR code image that contain a string. I wonder if is there any simple way to convert an image to QR code.

this is the implementation so far:


 from PIL import
 import qrcode

 def creatig_qr_code():

    qr = qrcode.make(
         "info_to_print"
    )

    qr.save('Element.png')

2
  • 2
    You may be able to convert the image to a base64 encoded string and then convert that string to QR code Commented Jun 22, 2022 at 9:06
  • 1
    What do you mean by "convert" an image? Read an existing image elsewhere, interpret it (read text I assume?) and then represent it as another image - but this time its a QR code containing the interpreted text? Or, as the others are assuming - you want it represented as an encoded string? Commented Jun 22, 2022 at 9:27

1 Answer 1

2

Try something like this:

import base64
import qrcode


with open("yourimage_path", "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())
qr = qrcode.QRCode()
qr.add_data(encoded_string)
qr.make()

and then save, or do whatever you want with the qr.

EDIT

The qrcode dimensions has to match the dimensions of the image you are trying to encode. Choosing a too tiny dimension will result in DataOverflowError.

Change the qr code instantiation qr = qrcode.QRCode() with:

qr = qrcode.QRCode(version={"correct version"}, error_correction=qrcode.constants.{"correct error correction type"})

choosing the values that fits you necessity from a table like this

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

3 Comments

I think maybe some libraries are missing, I have errors in many lines which are not exist
What do you mean? Paste the error you are getting
Traceback (most recent call last): File "C:\Users\e174701\OneDrive - Applied Materials\Desktop\QRCODE\draft.py", line 12, in <module> qr.make() File "C:\Users\e174701\AppData\Local\Programs\Python\Python39\lib\site-packages\qrcode\main.py", line 103, in make self.best_fit(start=self.version) File "C:\Users\e174701\AppData\Local\Programs\Python\Python39\lib\site-packages\qrcode\main.py", line 180, in best_fit raise exceptions.DataOverflowError() qrcode.exceptions.DataOverflowError

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.