1.问题或需求描述:
Python 生成纯色或渐变色图片
2.解决方法或原理:
python 代码
import numpy as np
from PIL import Image
def RGB(r,g,b): return (r,g,b)
def Make_img_data(width, height, rgb):
'''Make image data'''
result = np.zeros((height, width, 3), dtype=np.uint8)
for i, v in enumerate(rgb):
result[:,:,i] = np.tile(np.linspace(v, v, width), (height, 1))
return result
def Make_gradation_img_data(width, height, rgb_start, rgb_stop, horizontal=(True, True, True)):
'''Make gradation image data'''
result = np.zeros((height, width, 3), dtype=np.uint8)
for i, (m,n,o) in enumerate(zip(rgb_start, rgb_stop, horizontal)):
if o:
result[:,:,i] = np.tile(np.linspace(m, n, width), (height, 1))
else:
result[:,:,i] = np.tile(np.linspace(m, n, width), (height, 1)).T
return result
MakeImg = lambda width, height, rgb: Image.fromarray(