【发布时间】:2020-02-08 06:24:43
【问题描述】:
我试图通过将numpy matrix 转换为图像来制作圆形图像,但是当输入为50 或更多时,图像上出现奇怪的缺失线条。我怎样才能解决这个问题?
输入决定矩阵的大小,50 的输入构成50 by 50 矩阵。我是初学者,这是我第一次问堆栈溢出的问题,所以请不要太苛刻:) 这是我的代码。
from PIL import Image
import itertools
np.set_printoptions(threshold=np.inf)
inp = int(input("Input size of matrix"))
dt = np.dtype(np.int8)
M = np.zeros((inp, inp), dtype=dt)
A = (list(itertools.product(range(0, inp), repeat=2)))
count1 = 0
for n in A:
x = (int(n[0]) / (inp - 1)) * 2
y = (int(n[1]) / (inp - 1)) * 2
if (x ** 2) + (y ** 2) - (2 * x) - (2 * y) <= -1:
M[int(x * (inp - 1)/2), int(y * (inp - 1)/2)] = 1
count1 += 1
print(M)
im = Image.fromarray(M * 255)
im.show()
print("Approximation of pi: " + str(4 * (count1 / inp ** 2))) ```
【问题讨论】:
-
我说奇怪的行只出现在等于或大于 50 的值上,但我意识到问题出现在 input = 48 上,而不是 input = 49 上,所以我不确定是什么发生