用python画渐变的圆_使用numpy绘制圆形渐变
由于對稱性,實際上只需要計算圖像256*256的四分之一,即64*64,然后將其逐塊旋轉90度,然后將它們組合起來。這樣,總時間是計算256×256像素的1/4倍。在
以下是示例。在import numpy as np
import matplotlib.pyplot as plt
##Just calculate 64*64
arr = np.zeros((64,64,3), dtype=np.uint8)
imgsize = arr.shape[:2]
innerColor = (0, 0, 0)
outerColor = (255, 255, 255)
for y in range(imgsize[1]):
for x in range(imgsize[0]):
#Find the distance to the corner
distanceToCenter = np.sqrt((x) ** 2 + (y - imgsize[1]) ** 2)
#Make it on a scale from 0 to 1innerColor
distanceToCenter = distanceToCenter / (np.sqrt(2) * imgsize[0])
#Calculate r, g, and b values
r = outerColor[0] * distanceToCenter + innerColor[0] * (1 - distanceToCenter)
g = outerColor[1] * distanceToCenter + innerColor[1] * (1 - distanceToCenter)
b = outerColor[2] * distanceToCenter + innerColor[2] * (1 - distanceToCenter)
# print r, g, b
arr[y, x] = (int(r), int(g), int(b))
#rotate and combine
arr1=arr
arr2=arr[::-1,:,:]
arr3=arr[::-1,::-1,:]
arr4=arr[::,::-1,:]
arr5=np.vstack([arr1,arr2])
arr6=np.vstack([arr4,arr3])
arr7=np.hstack([arr6,arr5])
plt.imshow(arr7, cmap='gray')
plt.show()
總結
以上是生活随笔為你收集整理的用python画渐变的圆_使用numpy绘制圆形渐变的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机管理也无法运行,【两种解决方法】任
- 下一篇: IDEA 载入jQuery的方法