Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Made this to assist in creating QR codes and searching for images containing readable QR codes.
- import qrcode, cv2, os, sys, shutil
- from PIL import Image
- copy_to_dest = './found'
- qr_img_path = './qr.png'
- class qr_code:
- def __init__(self, data):
- # Create the QR code.
- self.qr = qrcode.QRCode(
- version=1,
- error_correction=qrcode.constants.ERROR_CORRECT_L,
- box_size=15, # Sets the size of the QR code modules to 15
- border=1, # Sets the border size to 1
- )
- # Add data to the QR code
- self.qr.add_data(data)
- self.qr.make(fit=True)
- def create_img(self, path):
- # Generate an image from the QR code instance
- self.img = self.qr.make_image(fill_color='black', back_color='white')
- self.img.save(path)
- class qr_img:
- def __init__(self, path):
- # Create a gray background image
- self.bg = Image.new('RGB', (768, 768), (128, 128, 128)) # Grey background
- self.img = Image.open(path)
- # Position the image in the center of the grey background.
- position = (
- (self.bg.width - self.img.width) // 2,
- (self.bg.height - self.img.height) // 2
- )
- self.bg.paste(self.img, position)
- self.bg.save(path)
- class scan_list:
- def __init__(self):
- self.name = []
- self.path = []
- def add_file(self, file_path, file_name):
- self.name.append(file_name)
- self.path.append(file_path)
- def __iter__(self):
- return zip(self.name, self.path).__iter__()
- def create_qr_code(text):
- # Create a QR image.
- x = qr_code(text)
- x.create_img(qr_img_path)
- #Paste QR into grey background image.
- y = qr_img(qr_img_path)
- y.bg.show()
- def read_qr_code(file_path):
- image = cv2.imread(file_path)
- detect_qr_code = cv2.QRCodeDetector()
- value = detect_qr_code.detectAndDecode(image)
- if value[1] is None:
- return False
- else:
- return True
- def get_files(path):
- # Get files in dir.
- with os.scandir(path) as entries:
- # Create scan list.
- x = scan_list()
- for entry in entries:
- if entry.is_file():
- try:
- # Add file to scan list.
- x.add_file(entry.path, entry.name)
- except:
- print(f'Unable to read {entry.name}')
- return x
- def main():
- print("Usage: To scan a directory for QR codes try 'python qr_helper.py /path/to/dir'\n")
- if len(sys.argv) != 2:
- # Create a QR code to drop into ControlNet.
- text = input('Enter a link or something to make a QR code: ')
- try:
- create_qr_code(text)
- except:
- print("Well that didn't work...")
- else:
- # Search a directory for scannable images. If found they will be copied to local dir.
- search_dir = sys.argv[1]
- print(f'Searching for QR codes in {search_dir}\n')
- # Make sure our local dir exists so we can copy images to it.
- if not os.path.exists(copy_to_dest):
- os.mkdir(copy_to_dest)
- try:
- # Create a list of files to be scanned.
- x = get_files(search_dir)
- # Try and read a QR code from each file in the list.
- for name, path in x:
- if read_qr_code(path):
- print(f'QR code detected: {name}')
- new_path = os.path.join(copy_to_dest, name)
- # If the image isn't already in the local dir then copy it.
- if not os.path.isfile(new_path):
- print(f'Copying {name}')
- shutil.copy(path, copy_to_dest)
- except:
- print("Well that didn't work...")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment