Skip to content

Instantly share code, notes, and snippets.

@addam
Created May 29, 2023 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save addam/20e8a8366590b4b6e228284dad1d06c1 to your computer and use it in GitHub Desktop.
Save addam/20e8a8366590b4b6e228284dad1d06c1 to your computer and use it in GitHub Desktop.
Read image from the clipboard and decode its qr code
#!/usr/bin/python3
from PyQt5.QtWidgets import QApplication, QLabel, QMessageBox
from pyzbar import pyzbar
import numpy as np
def qimage2mat(qimage):
qimage.convertToFormat(4)
ptr = qimage.bits()
ptr.setsize(qimage.byteCount())
arr = np.array(ptr).reshape([qimage.height(), qimage.width(), 4])
return arr
def messagebox(text, is_error=False):
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical if is_error else QMessageBox.Information)
msg.setText(text)
msg.setWindowTitle("Error" if is_error else "Information")
msg.show()
msg.exec_()
app = QApplication([])
clipboard = app.clipboard()
qimage = clipboard.image()
if not qimage.bits():
messagebox("No image in clipboard", True)
exit(1)
img = qimage2mat(qimage)
qrs = pyzbar.decode(img)
if qrs:
texts = [qr.data.decode() for qr in qrs]
clipboard.setText(texts[0])
messagebox("\n".join(texts) + "\nCopied to clipboard")
else:
messagebox("No QR code found", True)
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment