I'm creating images using Python, using
myImage = Image.new('RGB', (250, 250), 'rgb(155,89,182)')
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?
Thank you in advance.
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')
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.
As I would do if they offer me the gradient data in x, y. Which would not be central.
This is all the data they give me:
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
and 3 colors:
A_COLOR:100600ff
B_COLOR: f7d32aff
FallOff_COLOR: f7d32aff
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With