0

I need to write a program, that makes random black and white squares, that looks like QR codes, but I can use only tkinter and random. I was trying something like this:

def qr():
x = random.randint(1, 21)
canvas.create_rectangle(x * 10, x * 10, x + 10, x + 10, fill = 'black')

but I really don't know how to write it. I know, it's easy for someone but I'm just a beginner with programming. It should be something like that:qrcode

So thank you for every answer and have a nice day.

  • 2
    [QR code](https://en.wikipedia.org/wiki/QR_code) isn't just a random set of squares. – Olvin Roght Feb 01 '22 at 14:29
  • In principle you are about to create your own 2d code format. But most common formats (QR / Datamatrix / Aztec / ..) have search patterns which allows the scanner to establish boundaries. You need to have that. Plus some checksum etc. Suggest you don't re-invent the wheel and use a standard library. – MyICQ Feb 01 '22 at 14:36
  • Not familiar with Tkinter. Just from the function's documentation, `create_rectangle` makes only one rectangle. I'm guessing that you're trying to put multiple random rectangles in a canvas. Then you have to calculate the coordinates (random coordinates) for each rectangle and put them repetitively in the canvas. Use a loop. If you don't absolutely have to use Tkinter, matplotlib would be better option for this, in my opinion. – Abdur Rakib Feb 01 '22 at 14:37
  • Can you clarify if it needs to be a valid QR code, or just a 21x21 block of random black and white squares? – Mark Setchell Feb 01 '22 at 14:43
  • it's just 21x21 black and white squares, not a QR code, I was wrong because name QR code was in task – Martin Majerech Feb 01 '22 at 14:49

2 Answers2

0

You can use matplotlib to print any matrix, including random matrices too.

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(0)  # make it reproducible

# random 0s and 1s with equal probability
rnd_matrix = np.random.randint(low=0, high=2, size=(10, 10))

# format the image
fig, ax = plt.subplots()
ax.spines['top'].set_visible(False)  # hide the frame
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
plt.xticks([])  # hide tics and their labels
plt.yticks([])
ax.set_aspect(1)  # make the shape square

# create the image
plt.imshow(rnd_matrix, cmap='Greys')

This produces an image like

random 10x10 matrix as a bw image

The idea is taken from another SO answer.

DanielTuzes
  • 2,494
  • 24
  • 40
0

It's quite simple with Numpy and PIL:

from PIL import Image
import numpy as np

# Create 21x21 grid of pixels, each being 0 or 255
pixels = np.random.randint(0,2, (21,21), np.uint8) * 255

# Make Numpy array of pixels into PIL Image and save
Image.fromarray(pixels).save('result.png')

enter image description here

Or, if you want it bigger, change the last line to:

Image.fromarray(pixels).resize((100,100), resample=Image.NEAREST).save('result.png')

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432