Created
April 5, 2016 14:43
-
-
Save kymtwyf/4b194ae91f43aff4883f82b8bebd9419 to your computer and use it in GitHub Desktop.
replace qrcode in image with another one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from sys import argv | |
import zbar | |
import qrcode | |
from PIL import Image | |
if len(argv) < 2: exit(1) | |
newUrl = argv[2] | |
scanner = zbar.ImageScanner() | |
scanner.parse_config('enable') | |
pil = Image.open(argv[1]).convert('L') | |
width, height = pil.size | |
raw = pil.tobytes() | |
image = zbar.Image(width, height, 'Y800', raw) | |
scanner.scan(image) | |
originCopy = pil.copy(); | |
# for every symbol in image caculate width and height ,generate the qrcode and resize it , paste the generated qrcode to origin location | |
for symbol in image: | |
qrWidth = bottomRightCorners[0] - topLeftCorners[0] | |
qr = qrcode.QRCode( | |
border = 0 | |
) | |
qr.add_data(newUrl) | |
qr.make(fit=True) | |
image = qr.make_image(); | |
resizedImage = image.resize((qrWidth, qrHeight)) | |
originCopy.paste(resizedImage, topLeftCorners); | |
originCopy.save("new.png"); | |
del(image) | |
#usage python replaceqr.py http://kymtwyf.github.io |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment