Reputation: 347
So I've got some code to create a qr code using Python. However, I am unsure on how to make the eyes are black, make it so the data points are circles and add the logo correctly. Also, if there is a different coding language that would do this better I am happy to learn.
Wanted:
Code Generates:
import pyqrcode
from PIL import Image
url = pyqrcode.QRCode('http://www.amazon.com',error = 'H')
url.png('test.png',scale=10)
im = Image.open('test.png')
im = im.convert("RGBA")
logo = Image.open('Facebook-logo (1).png')
box = (135,135,235,235)
im.crop(box)
region = logo
region = region.resize((box[2] - box[0], box[3] - box[1]))
im.paste(region,box)
im.show()
Upvotes: 3
Views: 4375
Reputation: 53
Short answer
I temporarily solved it by writing a custom function to mask the eyes for me based on the standards for QRCode eyes:
from PIL import Image, ImageDraw
def style_eyes(img):
img_size = img.size[0]
eye_size = 70 #default
quiet_zone = 40 #default
mask = Image.new('L', img.size, 0)
draw = ImageDraw.Draw(mask)
draw.rectangle((40, 40, 110, 110), fill=255)
draw.rectangle((img_size-110, 40, img_size-40, 110), fill=255)
draw.rectangle((40, img_size-110, 110, img_size-40), fill=255)
return mask
Then combining the two images:
mask = style_eyes(qr_img)
final_img = Image.composite(qr_eyes_img, qr_img, mask)
final_img
Long answer
I don't know of functionality for this yet but you can follow the issue on GitHub
I used qrcode to generate my images instead of pyqrcode as you have there because they have the circular module functionality. You can find their documentation in the link for other styling properties. Here is my complete code:
import qrcode
from qrcode.image.styles.moduledrawers import RoundedModuleDrawer
from qrcode.image.styles.moduledrawers import CircleModuleDrawer
from qrcode.image.styles.colormasks import SolidFillColorMask
from PIL import Image, ImageDraw
def style_eyes(img):
img_size = img.size[0]
eye_size = 70 #default
quiet_zone = 40 #default
mask = Image.new('L', img.size, 0)
draw = ImageDraw.Draw(mask)
draw.rectangle((40, 40, 110, 110), fill=255)
draw.rectangle((img_size-110, 40, img_size-40, 110), fill=255)
draw.rectangle((40, img_size-110, 110, img_size-40), fill=255)
return mask
qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
qr.add_data('http://www.amazon.com')
qr_eyes_img = qr.make_image(image_factory=StyledPilImage,
eye_drawer=RoundedModuleDrawer(radius_ratio=1.2),
color_mask=SolidFillColorMask(back_color=(255, 255, 255), front_color=(255, 110, 0)))
qr_img = qr.make_image(image_factory=StyledPilImage,
module_drawer=CircleModuleDrawer(),
color_mask=SolidFillColorMask(front_color=(59, 89, 152)),
embeded_image_path="/Facebook-logo (1).png")
mask = style_eyes(qr_img)
final_img = Image.composite(qr_eyes_img, qr_img, mask)
Upvotes: 3