Instantly share code, notes, and snippets.

# framirez/qr_crop_random.py

Created January 24, 2018 11:39
Show Gist options
• Save framirez/e848b4daa824d29885f127d4dfdc9e11 to your computer and use it in GitHub Desktop.
Divide un QR en trozos y ciertas posiciones las permuta hasta obtener un resultado QR válido
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 from PIL import Image, ImageDraw import qrtools import random import math fixed_pic = [[2,3,6,8,11],[0,1,12,15,3]] #Se mueven las imagenes indice [0] de la lista a indice [1] permit_pic = range(16) for row in fixed_pic[1]: permit_pic.remove(row) print("Se van a permutar las siguientes posiciones {}".format(str(permit_pic))) def move_mosaic(a, fixed_pic): """ Mueve segun las permutaciones que definimos para reducir la probabilidad """ for row in range(len(fixed_pic[0])): aux = a[fixed_pic[1][row]] a[fixed_pic[1][row]] = a[fixed_pic[0][row]] a[fixed_pic[0][row]] = aux return a def randomList(a, permit_pic): """ Mueve de los cuadros permitidos un cuadro """ magic1 = random.choice(permit_pic) magic2 = random.choice(permit_pic) aux = a[magic1] a[magic1] = a[magic2] a[magic2] = aux return a img = Image.open("qr_mosaic.jpg") # se define alto y ancho de las cuadriculas size_w = img.size[0]/4 size_h = img.size[1]/4 print("Se corta imagen") images_array = [] for row in range(4): for column in range(4): #Cortamos la imagen y guardamos los trozos en una lsta print("Row {} column {} - params {} {} {} {} ".format(row, column, size_h*row, size_w*column, size_h+(size_h*row), size_w+(size_w*column))) img_aux = img.crop((size_h*row, size_w*column, size_h+(size_h*row), size_w+(size_w*column))) img_aux.save("qr/img{}{}.jpg".format(row, column))# Para ver que estan todo bien images_array.append(img_aux) images_array = move_mosaic(images_array, fixed_pic) cont_c = 0 while 1: mosaic = Image.new("RGB", (img.size[0], img.size[1]), "white") list_gen = images_array list_gen = randomList(images_array, permit_pic) cont = 0 for row in range(4): for column in range(4): mosaic.paste(list_gen[cont],(size_h*row, size_w*column, size_h+(size_h*row), size_w+(size_w*column))) cont = cont + 1 mosaic.save("qr/compose_aux.png") qr = qrtools.QR() if qr.decode("qr/compose_aux.png"): print("Contador: {} Total: {} Data: {}".format(cont_c, math.factorial(len(permit_pic)), qr.data)) raw_input() cont_c = cont_c + 1