├── example.py ├── example └── __init__.py ├── main.py ├── readme.md └── requirements.txt /example.py: -------------------------------------------------------------------------------- 1 | from path import Path 2 | from main import gen_qr_code 3 | 4 | 5 | text = "link" 6 | path_to_download = Path().joinpath("example", "Logo") # Путь до фона qr кода 7 | path_to_save = Path().joinpath("example", "example122.png") # Куда сохранять результат и под каким именем (обязательно в png) 8 | 9 | gen_qr_code(text, path_to_download, path_to_save) -------------------------------------------------------------------------------- /example/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orasept77/pillow_matrix_image/da32ba14061eb41a16ff6e605332d245d18ea121/example/__init__.py -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import qrcode 2 | from PIL import Image, ImageDraw 3 | from pyzbar.pyzbar import decode 4 | from path import Path 5 | 6 | 7 | # Путь до картинки с QR кодом 8 | def read_qr_code(path_to_download: Path): 9 | try: 10 | img = Image.open(path_to_download) 11 | decoded = decode(img) 12 | wrote = decoded[0].data.decode("utf-8") 13 | except: 14 | wrote = None 15 | return wrote 16 | 17 | 18 | def gen_qr_code(text: str, path_to_download: Path, path_to_save: Path = None): 19 | qr = qrcode.QRCode( 20 | version=1, 21 | error_correction=qrcode.constants.ERROR_CORRECT_H, 22 | box_size=10, 23 | border=1, 24 | ) 25 | qr.add_data(text) 26 | qr.make(fit=True) 27 | img = qr.get_matrix() 28 | 29 | coeff = 20 30 | coeff_small = round(coeff / 3) 31 | length_qr = len(img) * coeff 32 | 33 | try: 34 | background = Image.open(path_to_download).resize((length_qr, length_qr)).convert("RGBA") 35 | except: 36 | return False 37 | 38 | back_im = Image.new('RGBA', (length_qr, length_qr), (0, 0, 0, 0)) 39 | 40 | black_1 = (0, 0, 0, 0) 41 | black_2 = (0, 0, 0, 230) 42 | white_1 = (255, 255, 255, 50) 43 | white_2 = (255, 255, 255, 230) 44 | 45 | white_3 = (0, 0, 0, 0) 46 | 47 | idraw = ImageDraw.Draw(back_im, "RGBA") 48 | 49 | x = 0 50 | y = 0 51 | for string in qr.get_matrix(): #Принимаем пиксели и помещаем их в матрицу 52 | this_str = '' #Имя присваимое матричным данным 53 | for i in string: 54 | if i: 55 | this_str += '1' 56 | # idraw.ellipse((x + coeff_small, y + coeff_small, x + coeff - coeff_small, y + coeff - coeff_small), 57 | # fill=black_2) 58 | 59 | # idraw.rectangle((x, y, x + coeff, y + coeff), fill=black_1) 60 | 61 | idraw.rectangle((x + coeff_small, y + coeff_small, x + coeff - coeff_small, y + coeff - coeff_small), 62 | fill=black_2) 63 | 64 | 65 | else: 66 | this_str += '0' 67 | # idraw.ellipse((x + coeff_small, y + coeff_small, x + coeff - coeff_small, y + coeff - coeff_small), 68 | # fill=white_2) 69 | # idraw.rectangle((x, y, x + coeff, y + coeff), fill=white_1) 70 | idraw.rectangle((x + coeff_small, y + coeff_small, x + coeff - coeff_small, y + coeff - coeff_small), 71 | fill=white_2) 72 | x += coeff 73 | x = 0 74 | y += coeff 75 | 76 | idraw.rectangle((0, 0, coeff * 9, coeff * 9), fill=white_1) 77 | idraw.rectangle((length_qr, 0, length_qr - coeff * 9, coeff * 9), fill=white_1) 78 | idraw.rectangle((0, length_qr, coeff * 9, length_qr - coeff * 9), fill=white_1) 79 | idraw.rectangle((length_qr - coeff * 10, length_qr - coeff * 9, length_qr - coeff * 6, length_qr - coeff * 6), 80 | fill=white_1) 81 | 82 | idraw.rectangle((coeff, coeff, coeff * 8, coeff * 2), fill=black_2) 83 | idraw.rectangle((length_qr - coeff * 8, coeff, length_qr - coeff, coeff * 2), fill=black_2) 84 | idraw.rectangle((coeff, coeff * 7, coeff * 8, coeff * 8), fill=black_2) 85 | idraw.rectangle((length_qr - coeff * 8, coeff * 7, length_qr - coeff, coeff * 8), fill=black_2) 86 | idraw.rectangle((coeff, length_qr - coeff * 7, coeff * 8, length_qr - coeff * 8), fill=black_2) 87 | idraw.rectangle((coeff, length_qr - coeff * 2, coeff * 8, length_qr - coeff), fill=black_2) 88 | idraw.rectangle((length_qr - coeff * 7, length_qr - coeff * 7, length_qr - coeff * 8, length_qr - coeff * 8), 89 | fill=black_2) 90 | idraw.rectangle((coeff * 3, coeff * 3, coeff * 6, coeff * 6), fill=black_2) 91 | idraw.rectangle((length_qr - coeff * 3, coeff * 3, length_qr - coeff * 6, coeff * 6), fill=black_2) 92 | idraw.rectangle((coeff * 3, length_qr - coeff * 3, coeff * 6, length_qr - coeff * 6), fill=black_2) 93 | idraw.rectangle((coeff, coeff, coeff * 2, coeff * 8), fill=black_2) 94 | idraw.rectangle((coeff * 7, coeff, coeff * 8, coeff * 8), fill=black_2) 95 | 96 | idraw.rectangle((length_qr - coeff, coeff, length_qr - coeff * 2, coeff * 8), fill=black_2) 97 | idraw.rectangle((length_qr - coeff * 7, coeff, length_qr - coeff * 8, coeff * 8), fill=black_2) 98 | 99 | idraw.rectangle((coeff, length_qr - coeff, coeff * 2, length_qr - coeff * 8), fill=black_2) 100 | idraw.rectangle((coeff * 7, length_qr - coeff, coeff * 8, length_qr - coeff * 8), fill=black_2) 101 | 102 | idraw.rectangle((length_qr - coeff * 10, length_qr - coeff * 10, length_qr - coeff * 9, length_qr - coeff * 5), 103 | fill=black_2) 104 | idraw.rectangle((length_qr - coeff * 6, length_qr - coeff * 10, length_qr - coeff * 5, length_qr - coeff * 5), 105 | fill=black_2) 106 | 107 | idraw.rectangle((length_qr - coeff * 6, length_qr - coeff * 10, length_qr - coeff * 10, length_qr - coeff * 9), 108 | fill=black_2) 109 | idraw.rectangle((length_qr - coeff * 6, length_qr - coeff * 6, length_qr - coeff * 10, length_qr - coeff * 5), 110 | fill=black_2) 111 | 112 | background.paste(back_im, (0, 0), back_im) 113 | if path_to_save is not None: 114 | path_to_download = path_to_save 115 | 116 | background.save(path_to_download) 117 | return True 118 | 119 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | code=QR code 2 | 3 | The project consists of two files: "example.py" and "main.py". The purpose of the project is to generate and read codes. The "main.py" file contains two functions: "read_code" and "gen_code". The "read_code" function takes a Path object as an argument, which is the path to the image with the code. The function opens the image, decodes the code using the "pyzbar" library, and returns the decoded data as a string. If the image cannot be opened or the decoding fails, the function returns None. 4 | 5 | The "gen_code" function takes two Path objects as arguments: the first is the path to the image that will be used as a background for the code, and the second is the path to which the generated code image will be saved. The function also takes a string that will be encoded in the code. The function creates a code using library and overlays it on top of the background image. The function returns True if the image was successfully created and False if there was an error. 6 | 7 | The code is generated with a box size of 10 and a border of 1. The function calculates the size of the code image based on the length of the encoded text and the size of the boxes. It then resizes the background image to match the size of the code image. 8 | The code is generated with black and white boxes. The function uses the "PIL" library to draw the code image. It loops through the pixels in the code and draws a black box for each "1" and a white box for each "0". It also adds four black boxes in the corners and four black boxes in the center to create a border around code. The code image is then saved to the specified path. 9 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | path==16.3.0 2 | Pillow==9.0.1 3 | pyzbar==0.1.8 4 | qrcode==7.3.1 5 | --------------------------------------------------------------------------------