簡體   English   中英

在 Python 中使用 PIL 繪制圓形漸變

[英]Plot circular gradients using PIL in Python

我正在使用 Python 創建圖像,使用

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

這實際上創建了圖像。 但是有沒有辦法創建一個帶有我選擇的顏色背景但帶有漸變的圖像? 我想選擇藍色作為我的顏色,然后,我想要圖像邊緣的深藍色和圖像中心的淺藍色。 使用簡單的 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')

對於每個像素,它outerColor根據像素到中心的距離將紅色、綠色和藍色值設置為介於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')

圖像樣本

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM