lincolnloop / python-qrcode

Python QR Code image generator
https://pypi.python.org/pypi/qrcode
Other
4.34k stars 664 forks source link

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

Closed water5 closed 3 years ago

water5 commented 3 years ago

Can I add image into QR code like below?

Image

KmolYuan commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

Why paste not appear in dir(my_code)?

maribedran commented 3 years ago

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

water5 commented 3 years ago
  1. Refer: https://github.com/lincolnloop/python-qrcode/issues/232#issuecomment-882525215

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 commented 3 years ago

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 commented 3 years ago

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