Python 如何改变渐变的颜色方向?
下面生成从左边缘开始的渐变Python 如何改变渐变的颜色方向?,python,python-imaging-library,Python,Python Imaging Library,下面生成从左边缘开始的渐变 #!/usr/bin/env python import math from PIL import Image im = Image.new('RGB', (300, 300)) ld = im.load() # A map of rgb points in your distribution # [distance, (r, g, b)] # distance is percentage from left edge heatmap = [ [0.0
#!/usr/bin/env python
import math
from PIL import Image
im = Image.new('RGB', (300, 300))
ld = im.load()
# A map of rgb points in your distribution
# [distance, (r, g, b)]
# distance is percentage from left edge
heatmap = [
[0.0, (0, 0, 0)],
[1.00, (0.8, 0.0, 1.0)],
]
def gaussian(x, a, b, c, d=0):
return a * math.exp(-(x - b) ** 2 / (2 * c ** 2)) + d
def pixel(x, width=100, map=[], spread=1):
width = float(width)
r = sum([gaussian(x, p[1][0], p[0] * width, width / (spread * len(map))) for p in map])
g = sum([gaussian(x, p[1][1], p[0] * width, width / (spread * len(map))) for p in map])
b = sum([gaussian(x, p[1][2], p[0] * width, width / (spread * len(map))) for p in map])
return min(1.0, r), min(1.0, g), min(1.0, b)
for x in range(im.size[0]):
r, g, b = pixel(x, width=300, map=heatmap)
r, g, b = [int(256 * v) for v in (r, g, b)]
for y in range(im.size[1]):
ld[x, y] = r, g, b
im.show()
如何更改渐变的开始位置,上面是从左到右,但如果我想从上到下,该怎么办?只需反转像素分配中的坐标即可:
ld[x, y] = r, g, b
变成
ld[y, x] = r, g, b