Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New feature request] Add image into QR code. #232

Closed
water5 opened this issue Jul 18, 2021 · 7 comments
Closed

[New feature request] Add image into QR code. #232

water5 opened this issue Jul 18, 2021 · 7 comments

Comments

@water5
Copy link

water5 commented Jul 18, 2021

Can I add image into QR code like below?

Image

@KmolYuan
Copy link

KmolYuan commented Jul 19, 2021

You can just use PIL's method if the image factory is PilImage, which is the default factory. In that case, my_qrcode is Image type as well.

import qrcode
from PIL import Image

my_qrcode = qrcode.make("Some data here")

image = Image.open('image.jpg').resize((w, h))  # Resize before you paste!

my_qrcode.paste(image, (x, y))  # Set the coordinate
my_qrcode.save("my_qrcode.jpg")

@maribedran
Copy link
Contributor

You can also use the new styled PIL image factory and embed an image directly: https://github.com/lincolnloop/python-qrcode/blob/master/README.rst#styled-image

@water5
Copy link
Author

water5 commented Jul 19, 2021

Why paste not appear in dir(my_code)?

@maribedran
Copy link
Contributor

I didn't understand the question. Can you elaborate? Maybe show the code you're trying.

@water5
Copy link
Author

water5 commented Jul 19, 2021

  1. Refer:
    [New feature request] Add image into QR code. #232 (comment)

import qrcode
qr = qrcode.make('embeded image test')

Why paste not appear in
dir(qr) ?

  1. I try:

import qrcode
from qrcode.image.styledpil import StyledPilImage

qr = qrcode.QRCode(error_correction = qrcode.constants.ERROR_CORRECT_L)
qr.add_data('embeded image test')
img = qr.make_image(image_factory = StyledPilImage, embeded_image_path = 'test_image.png')

The two ways above, the image just override origin qrcode pixel, not 'embeded', so it can't Recognition,

Image

Image

@KmolYuan
Copy link

Why paste not appear in dir(qr)?

The qrcode object is actually the PilImage class, but it overrides __getattr__ magic method used to wrap the PIL image instance.

For overlay a transparent image, these methods should help. Please try to make the two pictures into the same mode via Image.convert, such as RGBA.

@water5
Copy link
Author

water5 commented Jul 20, 2021

Solved, reduce embed image size and set higher error correction level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants