You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

50 lines
1.3 KiB

from PIL import Image
from enum import Enum
import random
class Module(Enum):
BLACK = (0, 0, 0, 255)
WHITE = (255, 255, 255, 255)
BLACK_HARDCODE = (34, 177, 76, 255)
WHITE_HARDCODE = (237, 28, 36, 255)
UNKNOWN = (0, 162, 232, 255)
def process_image(image):
black_modules = 0
total_modules = 0
unknown = []
width, height = image.size
for y in range(height):
for x in range(width):
module = Module(image.getpixel((x, y)))
if module == Module.BLACK: # Known black modules
black_modules += 1
total_modules += 1
elif module == Module.WHITE: # Known white modules
total_modules += 1
elif module == Module.BLACK_HARDCODE: # Black modules enforced by QR code standand
image.putpixel((x,y), Module.BLACK.value)
elif module == Module.WHITE_HARDCODE: # White modules enforced by QR code standard
image.putpixel((x,y), Module.WHITE.value)
elif module == Module.UNKNOWN: # Unknown modules
unknown.append((x,y))
return black_modules/total_modules, unknown
def main():
template = Image.open('img/template.png')
ratio, unknown = process_image(template)
attempt = template.copy()
while True:
for coords in unknown:
attempt.putpixel(coords, Module.BLACK.value if random.random() < ratio else Module.WHITE.value)
attempt.resize((d*15 for d in attempt.size), Image.NEAREST).show()
input()
if __name__ == '__main__':
main()