More details on Image.linear_gradient #6276
-
Since Andrew so kindly answered my previous question about a progress bar, I am trying to learn more about linear_gradient. The docs seem a bit bland and bare - am I looking in the right place? What is "mode"? What are the range of values? Or maybe this is common knowledge and I simply missing it. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
For information about mode, see https://pillow.readthedocs.io/en/latest/handbook/concepts.html#modes
To generate a horizontal gradient from red to yellow in my previous example,
That was just me putting together a quick example. If you want to create a more complex linear gradient, it would probably be simpler to use from PIL import Image
def gradient(size, start, stop):
im = Image.new("RGB", size)
for x in range(im.width):
progress = x / (im.width - 1)
color = tuple(int(start[i] * (1 - progress) + stop[i] * progress) for i in range(3))
for y in range(im.height):
im.putpixel((x, y), color)
return im
im = gradient((256, 50), (255, 0, 0), (255, 255, 0))
im.show() |
Beta Was this translation helpful? Give feedback.
For information about mode, see https://pillow.readthedocs.io/en/latest/handbook/concepts.html#modes
linear_gradient
is not a very flexible function. When the docs say "Generate 256x256 linear gradient from black to white, top to bottom.", that's not just the default functionality, that's the entire functionality.To generate a horizontal gradient from red to yellow in my previous example,
merge()
to have a solid red channel, treat the gradient as the green channel, and an empty blue channel. That meant that the gradient went from full re…