[英]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.