Skip to content

Instantly share code, notes, and snippets.

@Apkawa
Created August 13, 2013 13:31
Show Gist options
  • Save Apkawa/6221083 to your computer and use it in GitHub Desktop.
Save Apkawa/6221083 to your computer and use it in GitHub Desktop.
how to do make gradient
from scipy.interpolate import griddata
import numpy as np
import Image as PILImage
def create_gradient(size, geometry):
"""
:param size: [x, y]
:param geometry:
{
"right_top: {
"coord": [x, y],
"color": '#FFF',
},
"left_top: {
"coord": [x, y],
"color": Color('#FFF'),
},
"right_bottom: {
"coord": [x, y],
"color": 'red',
},
"left_bottom": {
"coord": [x, y],
"color": [r, g, b],
},
}
:return:
"""
width, height = size
grid_x, grid_y = np.mgrid[0:width, 0:height]
points = np.array([
geometry['right_top']['coord'],
geometry['right_bottom']['coord'],
geometry['left_bottom']['coord'],
geometry['left_top']['coord'],
])
result_channels = []
colors = [
Color(geometry['right_top']['color']),
Color(geometry['right_bottom']['color']),
Color(geometry['left_bottom']['color']),
Color(geometry['left_top']['color']),
]
for channel_color in ['red', 'green', 'blue']:
values = np.array(map(lambda c: getattr(c, channel_color + '_int8'), colors ))
grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic')
pil_layer = PILImage.fromarray(grid_z2)
result_channels.append(pil_layer.convert("L"))
imRGB = PILImage.merge('RGB', result_channels)
return imRGB
if __name__ == '__main__':
geometry = {
"right_top": {
"coord": [0, 0],
'color': '#F00',
},
"left_top": {
"coord": [999, 0],
'color': '#000',
},
"right_bottom": {
"coord": [0, 999],
'color': '#000',
},
"left_bottom": {
"coord": [999, 999],
'color': '#FFF',
},
}
create_gradient(size=[1000, 1000], geometry=geometry).show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment