用圆锥形渐变填充矩阵

时间:2020-08-11 15:05:24

标签: python shapes grayscale graphing

我是Python的新手,想要一些建议。我将一些代码放在一起以生成具有锥形灰度渐变的图像。因此,黑色中心向白色过渡过渡为放射状。我已经建立了一个零维度的矩阵,它的尺寸是我想要的和循环的,我将用0到255的值填充这个矩阵,但是我不确定设置初始循环后该怎么做。我看过一些为矩阵的每个空间分配RGB值的代码,但是这种方法不适用于我拥有的后者应用程序。因此,我希望有一个0到255值的最终矩阵,可以将其绘制成灰度图。 这就是我到目前为止所拥有的...

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

Pi = 3.14159265359
twpi = 150
#Set up an all zero  matrix 12080 by 1024 with a depth 3
arr = np.zeros((1272,1024), dtype=np.int64)



#set the final image set to nbe the size oft he first two colmns of the blank array
imgsize = arr.shape[:2]

MiniVal = 0
MaxVal = 255


height = imgsize[1]
width = imgsize[0]

for y in range(height):
for x in range(width):
   
    
    #Find the distance to the center
    distanceToCenter = np.sqrt((x - width//2) ** 2 + (y - height//2) ** 2)
    
    #Make it on a scale from 0 to 1innerColor
    distanceToCenter = distanceToCenter / (np.sqrt(2) * width/2)
   
   
    fill = MaxVal * distanceToCenter + MiniVal * (1 - distanceToCenter)
    
    arr[y, x] = (fill)

干杯们,谢谢您的帮助。

0 个答案:

没有答案