[英]How can I create random QR code in python
您可以使用 matplotlib 打印任何矩陣,包括隨機矩陣。
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')
這會產生一個像
這個想法來自另一個 SO answer 。
使用 Numpy 和 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')
或者,如果您想要更大,請將最后一行更改為:
Image.fromarray(pixels).resize((100,100), resample=Image.NEAREST).save('result.png')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.