Closed water5 closed 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")
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
Why paste
not appear in dir(my_code)
?
I didn't understand the question. Can you elaborate? Maybe show the code you're trying.
import qrcode
qr = qrcode.make('embeded image test')
Why paste
not appear in
dir(qr)
?
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,
Why
paste
not appear indir(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.
Image.paste
(mask option)Image.alpha_composite
Solved, reduce embed image size and set higher error correction level.
Can I add image into QR code like below?