heuer/segno

Question: add logo in the center of qr code?

Closed this issue · 1 comments

Hello,

is it possible to add a logo in the middle of the generated QR code?
maybe using an H error (so we can remove 30% of the qr code)
example:
image

edit: this is my solution (adapted from stackoverflow)

import segno
from PIL import Image

URL = 'text'
LOGO = 'logo.png'
OUTPUT = 'qrcode.png'

# Make QR code
qr = segno.make_qr(URL, error='H')
qr.save(OUTPUT, finder_dark='#df2037', scale=100)

# Now open that png image to put the logo
img = Image.open(OUTPUT).convert("RGBA")

width, height = img.size

# How big the logo we want to put in the qr code png
logo_size = 1100

# Open the logo image
logo = Image.open(LOGO).convert("RGBA")

# Calculate xmin, ymin, xmax, ymax to put the logo
xmin = ymin = int((width / 2) - (logo_size / 2))
xmax = ymax = int((width / 2) + (logo_size / 2))

# resize the logo as calculated
logo = logo.resize((xmax - xmin, ymax - ymin))

# put the logo in the qr code
img.paste(logo, (xmin, ymin, xmax, ymax))

#img.show()
img.save(OUTPUT)

suggestions are welcomed

heuer commented

The lib does not provide such functionality out of the box and I think that's behind the scope of this lib, but you can, as shown, use Pillow.

The 1st step (saving the image to the file system) isn't necessary, though. IMO it's more elegant to save it to a file buffer.

import io
from PIL import Image
import segno


qr = segno.make_qr(URL, error='H')
out = io.BytesIO()
qr.save(out, kind='png', finder_dark='#df2037', scale=100)
out.seek(0)
img = Image.open(out)
[...]

Alternative: Use the qrcode-artistic -plugin, it can return a Pillow image and may save some code:

from PIL import Image
import segno

qr = segno.make_qr(URL, error='H')
img = qr.to_pil(finder_dark='#df2037', scale=100).convert('RGBA')
[...]