在 Python 中使用 PIL 绘制圆形梯度

Plot circular gradients using PIL in Python

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

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

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

提前谢谢你。

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

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

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

这将是圆形渐变的代码:

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')

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

围绕 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

如果他们向我提供 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