sukitfup

QR Code Helper

Sep 26th, 2023
5,641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.86 KB | Source Code | 0 0
  1. # Made this to assist in creating QR codes and searching for images containing readable QR codes.
  2.  
  3. import qrcode, cv2, os, sys, shutil
  4. from PIL import Image
  5.  
  6. copy_to_dest = './found'
  7. qr_img_path = './qr.png'
  8.  
  9. class qr_code:
  10.     def __init__(self, data):
  11.         # Create the QR code.
  12.         self.qr = qrcode.QRCode(
  13.             version=1,
  14.             error_correction=qrcode.constants.ERROR_CORRECT_L,
  15.             box_size=15,  # Sets the size of the QR code modules to 15
  16.             border=1,     # Sets the border size to 1
  17.         )
  18.         # Add data to the QR code
  19.         self.qr.add_data(data)
  20.         self.qr.make(fit=True)
  21.  
  22.     def create_img(self, path):
  23.         # Generate an image from the QR code instance
  24.         self.img = self.qr.make_image(fill_color='black', back_color='white')
  25.         self.img.save(path)
  26.  
  27. class qr_img:
  28.     def __init__(self, path):
  29.         # Create a gray background image
  30.         self.bg = Image.new('RGB', (768, 768), (128, 128, 128))  # Grey background
  31.         self.img = Image.open(path)
  32.         # Position the image in the center of the grey background.
  33.         position = (
  34.             (self.bg.width - self.img.width) // 2,
  35.             (self.bg.height - self.img.height) // 2
  36.         )
  37.         self.bg.paste(self.img, position)
  38.         self.bg.save(path)
  39.  
  40. class scan_list:
  41.     def __init__(self):
  42.         self.name = []
  43.         self.path = []
  44.  
  45.     def add_file(self, file_path, file_name):
  46.         self.name.append(file_name)
  47.         self.path.append(file_path)
  48.  
  49.     def __iter__(self):
  50.         return zip(self.name, self.path).__iter__()
  51.  
  52. def create_qr_code(text):
  53.     # Create a QR image.
  54.     x = qr_code(text)
  55.     x.create_img(qr_img_path)
  56.     #Paste QR into grey background image.
  57.     y = qr_img(qr_img_path)
  58.     y.bg.show()
  59.  
  60. def read_qr_code(file_path):
  61.     image = cv2.imread(file_path)
  62.     detect_qr_code = cv2.QRCodeDetector()
  63.     value = detect_qr_code.detectAndDecode(image)  
  64.     if value[1] is None:
  65.         return False
  66.     else:
  67.         return True
  68.  
  69. def get_files(path):
  70.     # Get files in dir.
  71.     with os.scandir(path) as entries:
  72.         # Create scan list.
  73.         x = scan_list()
  74.         for entry in entries:
  75.             if entry.is_file():
  76.                 try:
  77.                     # Add file to scan list.
  78.                     x.add_file(entry.path, entry.name)
  79.                 except:
  80.                     print(f'Unable to read {entry.name}')
  81.         return x
  82.  
  83. def main():
  84.     print("Usage: To scan a directory for QR codes try 'python qr_helper.py /path/to/dir'\n")
  85.     if len(sys.argv) != 2:
  86.         # Create a QR code to drop into ControlNet.
  87.         text = input('Enter a link or something to make a QR code: ')
  88.         try:
  89.             create_qr_code(text)
  90.         except:
  91.             print("Well that didn't work...")
  92.     else:
  93.         # Search a directory for scannable images. If found they will be copied to local dir.
  94.         search_dir = sys.argv[1]
  95.         print(f'Searching for QR codes in {search_dir}\n')
  96.         # Make sure our local dir exists so we can copy images to it.
  97.         if not os.path.exists(copy_to_dest):
  98.             os.mkdir(copy_to_dest)
  99.         try:
  100.             # Create a list of files to be scanned.
  101.             x = get_files(search_dir)
  102.             # Try and read a QR code from each file in the list.
  103.             for name, path in x:
  104.                 if read_qr_code(path):
  105.                     print(f'QR code detected: {name}')
  106.                     new_path = os.path.join(copy_to_dest, name)
  107.                     # If the image isn't already in the local dir then copy it.
  108.                     if not os.path.isfile(new_path):
  109.                         print(f'Copying {name}')
  110.                         shutil.copy(path, copy_to_dest)
  111.         except:
  112.             print("Well that didn't work...")
  113.  
  114. if __name__ == "__main__":
  115.     main()
  116.  
Advertisement
Add Comment
Please, Sign In to add comment