'Is there any fast way to draw gradients with color given by a 2D function in Python? [closed]
So I'm creating a small program for physics simulations in Python. Basically it simulates the evolution of some fields and points based on some differential equations. I made some simple visualisation of what is happening using pygame, and it worked well (it's just drawing circles, so nothing special). But then I wanted to also render field values. I tried using a pygame surface with color values determined by a field value at that point, but it's just too slow, even after changing the code so that I don't iterate over anything and instead do all operations on numpy arrays.
So my question is: can this be done faster in pygame, and if not, what other graphical library would be best for that purpose?
Solution 1:[1]
if you want to create gradient image use Pillow module:
from PIL import Image, ImageDraw
height, width = 800, 500 # the height and the width of the image
image = Image.new('RGB', (width, height), '#FFFFFF')
a, b, c = 0, 113, 189 # first color in rgb format
x, y, z = 181, 217, 231 # second color in rgb format
for i in range(height):
a, b, c = a + (x - a) / 500., b + (y - b) / 500., c + (z - c) / 500.
ImageDraw.Draw(image).line((0, i, width, i), fill=(int(a), int(b), int(c)))
image.save('image.png', 'PNG')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

