在Python中使用PIL绘制圆形渐变 [英] Plot circular gradients using PIL in Python

查看:1139
本文介绍了在Python中使用PIL绘制圆形渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating images using Python, using

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

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

and this actually creates the image. But is there a way to create an image with a background of the color I'm choosing but with gradients? I want to pick blue as my color, then, I want deep blue in the edges and more light blue in the center of the image. Is that possible using simple PIL and Python?

提前谢谢你。

推荐答案

代码取决于你希望渐变的样子。


你可以制作它是一个矩形渐变,看起来像这样:


The code depends on how you want the gradient to look.

You could make it a rectangular gradient which would look like this:

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


Or you could make it a round gradient like this:

这将是圆形渐变的代码:

This would be the code for the round gradient:

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

对于每个像素,它将红色,绿色和蓝色值设置在<$ c之间的某处$ c> innerColor 和 outerColor 取决于像素到中心的距离。


这将是矩形渐变的代码:

For each pixel, it sets the red, green, and blue values somewhere in between innerColor and outerColor depending on the distance from the pixel to the center.

This would be the code for the rectangular gradient:

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

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

This works the same way, except it measures the distance to the closest edge, not the center.

这篇关于在Python中使用PIL绘制圆形渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆