# -*- coding: utf-8 -*-
"""
Created on 2017/7/25
@author: ybwang
"""
from PIL import Image
import numpy as np
img = Image.new("RGBA", (255, 255), (255, 0, 0))
pima = img.load()
# red yellow green
for i in range(255):
r = 255 if i < 127 else 255 - int((i - 127) / float(254 - 127) * 255)
g = int(i / float(127) * 255) if i < 127 else 255
# print(i, r, g)
if r < 0: r = 0
if r > 255: r = 255
if g < 0: g = 0
if g > 255: g = 255
# for j in range(255):
# pima[i, j] = (r, g, 0)
# red yellow blue
for i in range(255):
r = 255 if i < 127 else 255 - int((i - 127) / (254 - 127) * 255)
g = int(i / float(127) * 255) if i <= 127 else 255 - int((i - 127) / (254 - 127) * 255)
b = 0 if i < 127 else int((i - 127) / (254 - 127) * 255)
# print(i, r, g, b)
if r < 0: r = 0
if r > 255: r = 255
if g < 0: g = 0
if g > 255: g = 255
if b < 0: b = 0
if b > 255: b = 255
for j in range(255):
pima[i, j] = (r, g, b)
def red_yellow_blue(dlist): # decrease
colors = []
min_d, max_d = min(dlist), max(dlist)
middle_index = int(len(dlist) / 2)
middle = sorted(dlist)[middle_index]
new_d = [(x - min_d) / (max_d - min_d) for x in sorted(dlist, reverse=True)]
for i, d in enumerate(sorted(dlist, reverse=True)):
nd = (d - min_d) / (middle - min_d) if d < middle else (d - middle) / (max_d - middle)
r = 255 if d >= middle else int(255 * nd)
g = int(255 * (1 - nd)) if d >= middle else int(255 * nd)
b = 0 if d >= middle else int(255 * (1 - nd))
print(i, r, g, b)
if r < 0: r = 0
if r > 255: r = 255
if g < 0: g = 0
if g > 255: g = 255
if b < 0: b = 0
if b > 255: b = 255
colors.append((r, g, b))
return colors
d = np.random.normal(0, 1, 100)
colors = red_yellow_blue(d)
# for i in colors:
# print(i)
# img.save('d:/xx.jpg')