You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
here is quick and dirty script I composed to progressively go through compression quality types while embedding an image , so I can be sure we could decode. But since this library is not decoder it would require external library.
mkqrcode.py script
#!/usr/bin/env python3importargparseimportloggingimportrefrompyzbar.pyzbarimportdecodefromPILimportImageimportqrcodefromqrcode.image.styledpilimportStyledPilImagefromqrcode.image.styles.moduledrawers.pilimportGappedSquareModuleDrawer# Create a dedicated loggerlgr=logging.getLogger(__name__)
defsanitize_filename(filename):
sanitized=re.sub(r'[^\w]', '_', filename)
returnsanitizeddefvalidate_compression_qualities(qualities):
valid_qualities= {'L', 'M', 'Q', 'H'}
ifnotall(qinvalid_qualitiesforqinqualities):
raiseValueError(f"Invalid compression qualities specified. Use any combination of: {', '.join(valid_qualities)}.")
returnqualitiesdefmain(text, input_image, output_image, compression_qualities):
lgr.debug(f"Processing text: {text}")
kws= {}
ifinput_image:
lgr.debug(f"Embedding image: {input_image}")
kws['embeded_image_path'] =input_imageforqincompression_qualities:
compress_quality=getattr(qrcode.constants, f'ERROR_CORRECT_{q}')
qr=qrcode.QRCode(error_correction=compress_quality)
qr.add_data(text)
img=qr.make_image(image_factory=StyledPilImage,
module_drawer=GappedSquareModuleDrawer(),
**kws)
img.save(output_image)
lgr.debug(f"Saved QR code image to: {output_image}")
decoded=decode(Image.open(output_image))
iflen(decoded) !=1:
lgr.debug(f"With error correction {q} got {len(decoded)} results.")
continuedecoded_data=decoded[0]
lgr.debug(f"DEBUG: error correction {q} - quality: {decoded_data.quality}")
ifdecoded_data.quality<0.5:
lgr.warning(f"With error correction {q} got too low quality {decoded_data.quality}.")
continuetext_decoded=decoded_data.data.decode()
iftext_decoded!=text:
lgr.warning(f"With error correction {q} decoded text does not match: {text_decoded!r} instead of {text!r}")
continuelgr.info(f"Successfully decoded text: {text_decoded!r} encoded with compress quality {q}. File {output_image}")
breakelse:
lgr.error(f"Neither of error correction levels was good enough. File {output_image} might be unreadable.")
if__name__=="__main__":
parser=argparse.ArgumentParser(description="Generate and decode QR codes.")
parser.add_argument("text", help="Text to encode in the QR code.")
parser.add_argument("-i", "--input-image", help="Path to the input image (optional).")
parser.add_argument("-o", "--output-image", help="Path to the output image (optional).")
parser.add_argument("-c", "--compression-qualities", default="LMQH",
help="Compression qualities (default: LMQH). Choose any combination of L, M, Q, H.")
parser.add_argument("-l", "--log-level", default="INFO", help="Set the logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL).")
args=parser.parse_args()
# Setup logginglogging.basicConfig(level=args.log_level.upper(), format='%(asctime)s - %(levelname)s - %(message)s')
# Validate compression qualitiestry:
compression_qualities=validate_compression_qualities(args.compression_qualities)
exceptValueErrorase:
lgr.error(e)
exit(1)
# Sanitize output filenameoutput_filename=args.output_imageifargs.output_imageelsef"{sanitize_filename(args.text)}.png"main(args.text, args.input_image, output_filename, compression_qualities)
and be sure that I can use that qr code. I am new to this project, and there are CLIs but since it would require library to decode, I wonder if something like that could still be added to the project instead of breeding one off script?
The text was updated successfully, but these errors were encountered:
here is quick and dirty script I composed to progressively go through compression quality types while embedding an image , so I can be sure we could decode. But since this library is not decoder it would require external library.
mkqrcode.py script
so I could invoke like
python tools/mkqrcode.py -i **/BIDS_logo_black_square_in_circle.png -o talks-qr.png https://bids-standard.github.io/bids-starter-kit/talks.html
and be sure that I can use that qr code. I am new to this project, and there are CLIs but since it would require library to decode, I wonder if something like that could still be added to the project instead of breeding one off script?
The text was updated successfully, but these errors were encountered: