【问题标题】:Plot circular gradients using PIL in Python在 Python 中使用 PIL 绘制圆形梯度
【发布时间】:2021-06-06 10:08:35
【问题描述】:

我正在使用 Python 创建图像,使用

myImage = Image.new('RGB', (250, 250), 'rgb(155,89,182)')

这实际上创建了图像。但是有没有办法用我选择的颜色的背景创建一个带有渐变的图像?我想选择 blue 作为我的颜色,然后,我希望图像边缘为深蓝色,而图像中心则为浅蓝色。使用简单的 PIL 和 Python 可以做到吗?

提前谢谢你。

【问题讨论】:

    标签: python image python-imaging-library


    【解决方案1】:

    代码取决于您希望渐变的外观。

    您可以将其设为如下所示的矩形渐变:

    或者你可以把它做成这样的圆形渐变:

    这将是圆形渐变的代码:

    import Image
    import math
    
    imgsize = (250, 250) #The size of the image
    
    image = Image.new('RGB', imgsize) #Create the image
    
    innerColor = [80, 80, 255] #Color at the center
    outerColor = [0, 0, 80] #Color at the corners
    
    
    for y in range(imgsize[1]):
        for x in range(imgsize[0]):
    
            #Find the distance to the center
            distanceToCenter = math.sqrt((x - imgsize[0]/2) ** 2 + (y - imgsize[1]/2) ** 2)
    
            #Make it on a scale from 0 to 1
            distanceToCenter = float(distanceToCenter) / (math.sqrt(2) * imgsize[0]/2)
    
            #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)
    
    
            #Place the pixel        
            image.putpixel((x, y), (int(r), int(g), int(b)))
    
    image.save('circlegradient.jpg')
    

    对于每个像素,它会根据像素到中心的距离将红色、绿色和蓝色值设置在 innerColorouterColor 之间。

    这将是矩形渐变的代码:

    import Image
    
    imgsize = (250, 250) #The size of the image
    
    image = Image.new('RGB', imgsize) #Create the image
    
    innerColor = [80, 80, 255] #Color at the center
    outerColor = [0, 0, 80] #Color at the edge
    
    
    for y in range(imgsize[1]):
        for x in range(imgsize[0]):
    
            #Find the distance to the closest edge
            distanceToEdge = min(abs(x - imgsize[0]), x, abs(y - imgsize[1]), y)
    
            #Make it on a scale from 0 to 1
            distanceToEdge = float(distanceToEdge) / (imgsize[0]/2)
    
            #Calculate r, g, and b values
            r = innerColor[0] * distanceToEdge + outerColor[0] * (1 - distanceToEdge)
            g = innerColor[1] * distanceToEdge + outerColor[1] * (1 - distanceToEdge)
            b = innerColor[2] * distanceToEdge + outerColor[2] * (1 - distanceToEdge)
    
    
            #Place the pixel        
            image.putpixel((x, y), (int(r), int(g), int(b)))
    
    image.save('rectgradient.jpg')
    

    除了测量到最近边缘的距离而不是中心之外,它的工作方式相同。

    【讨论】:

    • 非常感谢!这就是我一直在寻找的答案! :)
    【解决方案2】:

    做一个围绕线 ax+by+c=0 的梯度

    a = -0.5
    b = -1
    c = 250
    width = 55
    
    imgsize = (180, 320) #The size of the image
    image = PIL.Image.new('RGB', imgsize, color="white") #Create the image
    
    innerColor = [255, 0, 0] #Color at the center
    outerColor = [0, 0, 0] #Color at the edge
    
    for y in range(imgsize[1]):
        for x in range(imgsize[0]):
    
            dist = (a*x + b*y + c)/np.sqrt(a*a+b*b)
            color_coef = abs(dist)/width
    
            if abs(dist) < width:
                red = outerColor[0] * color_coef + innerColor[0] * (1 - color_coef)
                green = outerColor[1] * color_coef + innerColor[1] * (1 - color_coef)
                blue = outerColor[2] * color_coef + innerColor[2] * (1 - color_coef)
    
                image.putpixel((x, y), (int(red), int(green), int(blue)))
    
    image.save('linegradient.jpg')
    

    image sample

    【讨论】:

      【解决方案3】:

      如果他们向我提供 x、y 中的梯度数据,我会这样做。这不会是中心。

      这是他们给我的所有数据:

      Spotlight_Size  90.81163
      RefractionDepthBias:    0
      GradientPOSX:   50
      GradientPOSY:   99.68244
      GradientSIZE:   121.87289
      Spotlight_Intensity:    105
      Spotlight_PoSX: 50.192413
      Spotlight_PosY: 52.344917
      FallOffColor_Fill_Percent:  40
      FallOffColor_Postion: 50
      

      和 3 种颜色:

      A_COLOR:100600ff
      B_COLOR: f7d32aff
      FallOff_COLOR: f7d32aff
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-16
        • 2014-07-15
        • 2013-08-19
        相关资源
        最近更新 更多