Python 生成纯色或渐变色图片
2020-11-15 14:29
1531 查看
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(Make_img_data(width, height, rgb)) MakeGradationImg = lambda width, height, rgb_start, rgb_stop, horizontal=(True, True, True): \ Image.fromarray(Make_gradation_img_data(width, height, rgb_start, rgb_stop, horizontal)) #Function Test img = MakeImg(400, 400, RGB(255,0,0)) #red img.save('red.png') #~ img.show() img = MakeImg(400, 400, RGB(0,255,0)) #green img.save('green.png') #~ img.show() img = MakeImg(400, 400, RGB(0,0,255)) #blue img.save('blue.png') #~ img.show() img = MakeGradationImg(400, 400, RGB(255,0,0), RGB(0,255,0), (True, True, True)) img.save('img_001.png') #~ img.show() img = MakeGradationImg(400, 400, RGB(255,0,0), RGB(0,255,0), (False, True, True)) img.save('img_002.png') #~ img.show() img = MakeGradationImg(400, 400, RGB(255,0,0), RGB(0,255,0), (False, False, True)) img.save('img_003.png') #~ img.show() img = MakeGradationImg(400, 400, RGB(255,0,0), RGB(0,255,0), (False, False, False)) img.save('img_004.png') #~ img.show()
3.运行结果
1.red.png:
2.green.png:
3.blue.png:
4.img_001.png:
5.img_002.png:
6.img_003.png:
7.img_004.png:
相关文章推荐
- 使用 Python 生成类数字字母混合验证码图片
- Python matplotlib生成图片背景透明
- Python下使用qrcode模块生成vCard电子名片二维码图片
- Python matplotlib生成图片背景透明
- python生成九宫格图片
- Python 模拟生成动态产生验证码图片的方法
- python 根据数组生成图片
- Python 生成类字母验证码图片
- 从零学python系列之教你如何根据图片生成字符画
- 使用Python生成一张用于登陆验证的字符图片
- python2 生成验证码图片
- Python matplotlib生成图片背景透明
- Python matplotlib生成图片背景透明
- Python利用matplotlib生成图片背景及图例透明的效果
- [Python常用操作]生成幽灵图片
- python生成图片
- Python批量生成幻影坦克图片实例代码
- Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed
- python生成带有表格的图片
- python+opencv3生成一个自定义纯色图教程