python渐变色代码_如何在Python中创建颜色渐变?
6 個答案:
答案 0 :(得分:54)
我還沒有看到一個簡單的答案就是使用colour package。
通過pip安裝
pip install colour
如此使用:
from colour import Color
red = Color("red")
colors = list(red.range_to(Color("green"),10))
# colors is now a list of length 10
# Containing:
# [, , , , , , , , , ]
將輸入更改為您想要的任何顏色
答案 1 :(得分:26)
很明顯,您的原始示例漸變不是線性的。查看圖像中平均紅色,綠色和藍色值的圖表:
嘗試使用線性漸變的組合來重新創建它將是困難的。
對我來說,每種顏色看起來都像是兩條高斯曲線的加法,所以我做了一些最佳配合,并提出了這個:
使用這些計算出的值,我可以創建一個非常漂亮的漸變,幾乎完全匹配你的。
import math
from PIL import Image
im = Image.new('RGB', (604, 62))
ld = im.load()
def gaussian(x, a, b, c, d=0):
return a * math.exp(-(x - b)**2 / (2 * c**2)) + d
for x in range(im.size[0]):
r = int(gaussian(x, 158.8242, 201, 87.0739) + gaussian(x, 158.8242, 402, 87.0739))
g = int(gaussian(x, 129.9851, 157.7571, 108.0298) + gaussian(x, 200.6831, 399.4535, 143.6828))
b = int(gaussian(x, 231.3135, 206.4774, 201.5447) + gaussian(x, 17.1017, 395.8819, 39.3148))
for y in range(im.size[1]):
ld[x, y] = (r, g, b)
不幸的是,我還不知道如何將其推廣為任意顏色。
答案 2 :(得分:6)
如果你只需要插入2種顏色,我就為此編寫了一個簡單的函數。 colorFader使用其他兩個十六進制顏色代碼創建一個十六進制顏色代碼。
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
def colorFader(c1,c2,mix=0): #fade (linear interpolate) from color c1 (at mix=0) to c2 (mix=1)
c1=np.array(mpl.colors.to_rgb(c1))
c2=np.array(mpl.colors.to_rgb(c2))
return mpl.colors.to_hex((1-mix)*c1 + mix*c2)
c1='#1f77b4' #blue
c2='green' #green
n=500
fig, ax = plt.subplots(figsize=(8, 5))
for x in range(n+1):
ax.axvline(x, color=colorFader(c1,c2,x/n), linewidth=4)
plt.show()
結果:強>
由于高興趣而更新:
colorFader現在適用于rgb-colors和顏色字符串,如“red”或“r”。
答案 3 :(得分:6)
每個元組的第一個元素(0,0.25,0.5等)是顏色應該是某個值的位置。我拿了5個樣本來查看RGB組件(在GIMP中),并將它們放在表格中。 RGB分量從0變為1,因此我必須將它們除以255.0以縮放正常的0-255值。
5分是一個相當粗略的近似值 - 如果你想要一個更平滑的'外觀,使用更多的價值觀。
from matplotlib import pyplot as plt
import matplotlib
import numpy as np
plt.figure()
a=np.outer(np.arange(0,1,0.01),np.ones(10))
fact = 1.0/255.0
cdict2 = {'red': [(0.0, 22*fact, 22*fact),
(0.25, 133*fact, 133*fact),
(0.5, 191*fact, 191*fact),
(0.75, 151*fact, 151*fact),
(1.0, 25*fact, 25*fact)],
'green': [(0.0, 65*fact, 65*fact),
(0.25, 182*fact, 182*fact),
(0.5, 217*fact, 217*fact),
(0.75, 203*fact, 203*fact),
(1.0, 88*fact, 88*fact)],
'blue': [(0.0, 153*fact, 153*fact),
(0.25, 222*fact, 222*fact),
(0.5, 214*fact, 214*fact),
(0.75, 143*fact, 143*fact),
(1.0, 40*fact, 40*fact)]}
my_cmap2 = matplotlib.colors.LinearSegmentedColormap('my_colormap2',cdict2,256)
plt.imshow(a,aspect='auto', cmap =my_cmap2)
plt.show()
請注意,紅色很明顯。它就在那里因為中心區域接近灰色 - 這三個組成部分是必要的。
這會產生:
答案 4 :(得分:5)
這會創建一個由單個參數y:控制的色彩映射
from matplotlib.colors import LinearSegmentedColormap
def bluegreen(y):
red = [(0.0, 0.0, 0.0), (0.5, y, y), (1.0, 0.0, 0.0)]
green = [(0.0, 0.0, 0.0), (0.5, y, y), (1.0, y, y)]
blue = [(0.0, y, y), (0.5, y, y),(1.0,0.0,0.0)]
colordict = dict(red=red, green=green, blue=blue)
bluegreenmap = LinearSegmentedColormap('bluegreen', colordict, 256)
return bluegreenmap
red從0上升到y,然后回到0. green從0上升到y然后是常量。 blue的{??{1}}星在y處出現,并且在上半場保持不變,然后降至0。
這里是y = 0.7:的情節
您可以使用添加另一個或兩個段來平滑它。
答案 5 :(得分:4)
我也需要這個,但我想輸入多個任意顏色點。考慮一個熱圖,你需要黑色,藍色,綠色...一直到#34;熱"顏色。我借用了Mark Ransom上面的代碼并將其擴展以滿足我的需求。我很滿意。我要感謝所有人,尤其是馬克。
此代碼與圖像大小無關(高斯分布中沒有常數);您可以使用width =參數將其更改為pixel()。它還允許調整"傳播" ( - > stddev)的分布;你可以通過將spread =參數更改為pixel()來進一步混淆它們或引入黑帶。
#!/usr/bin/env python
import math
from PIL import Image
im = Image.new('RGB', (3000, 2000))
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)],
[0.20, (0, 0, .5)],
[0.40, (0, .5, 0)],
[0.60, (.5, 0, 0)],
[0.80, (.75, .75, 0)],
[0.90, (1.0, .75, 0)],
[1.00, (1.0, 1.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=3000, 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.save('grad.png')
總結
以上是生活随笔為你收集整理的python渐变色代码_如何在Python中创建颜色渐变?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中math库_Python的
- 下一篇: 柱状图python_python柱状图一