在做色彩相关的算法分析时候,经常需要使用规则的颜色表来进行辅助。下面用python(numpy和opencv)来生成颜色表并保存为图片。

有两种类型:

  • 格子形状的颜色表
  • 渐变色带

长的样子分别如下:

格子颜色表

这里需要注意,当划分的颜色数量比较少时,最好把一个颜色像素扩展成为一个格子,不然的话整个图看起来就太小了。

# -*- coding: utf-8 -*-
import cv2
import numpy as npdef generate_color_chart(block_num=18,block_columns=6,grid_width=32,grid_height=None):"""Generate color chart by uniformly distributed color indexes, only support8 bit (uint8).Parameters----------block_num: Block number of color chart, also the number of color indexes.block_columns: Column number of color chart. Row number is computed byblock_num / block_columnsgrid_width: Width of color gridgrid_height: Height of color grid. If not set, it will equal to grid_width."""color_index = np.linspace(0, 255, block_num)color_index = np.uint8(np.round(color_index))if grid_height is None:grid_height = grid_width# compute sizesblock_rows = np.int_(np.ceil(block_num / block_columns))block_width = grid_width * block_numblock_height = grid_height * block_numwidth = block_width * block_columnsheight = block_height * block_rowsresult = np.zeros((height, width, 3), dtype=np.uint8)# compute red-green block, (blue will be combined afterward)red_block, green_block = np.meshgrid(color_index, color_index)red_block = expand_pixel_to_grid(red_block, grid_width, grid_height)green_block = expand_pixel_to_grid(green_block, grid_width, grid_height)rg_block = np.concatenate([red_block, green_block], axis=2)# combine blue channelfor i in range(block_num):blue = np.ones_like(rg_block[..., 0], dtype=np.uint8) * color_index[i]color_block = np.concatenate([rg_block, blue[..., np.newaxis]], axis=2)# compute block indexblock_row = i // block_columnsblock_column = i % block_columnsxmin = block_column * block_widthymin = block_row * block_heightxmax = xmin + block_widthymax = ymin + block_heightresult[ymin:ymax, xmin:xmax, :] = color_blockresult = result[..., ::-1]  # convert from rgb to bgrreturn resultdef expand_pixel_to_grid(matrix, grid_width, grid_height):"""Expand a pixel to a grid. Inside the grid, every pixel have the same valueas the source pixel.Parameters----------matrix: 2D numpy arraygrid_width: width of gridgrid_height: height of grid"""height, width = matrix.shape[:2]new_heigt = height * grid_heightnew_width = width * grid_widthrepeat_num = grid_width * grid_heightmatrix = np.expand_dims(matrix, axis=2).repeat(repeat_num, axis=2)matrix = np.reshape(matrix, (height, width, grid_height, grid_width))# put `height` and `grid_height` axes together;# put `width` and `grid_width` axes together.matrix = np.transpose(matrix, (0, 2, 1, 3))matrix = np.reshape(matrix, (new_heigt, new_width, 1))return matrixif __name__ == '__main__':color_chart16 = generate_color_chart(block_num=16,grid_width=32,block_columns=4)color_chart18 = generate_color_chart(block_num=18,grid_width=32,block_columns=6)color_chart36 = generate_color_chart(block_num=36,grid_width=16,block_columns=6)color_chart52 = generate_color_chart(block_num=52,grid_width=8,block_columns=13)color_chart256 = generate_color_chart(block_num=256,grid_width=1,block_columns=16)cv2.imwrite('color_chart16.png', color_chart16)cv2.imwrite('color_chart18.png', color_chart18)cv2.imwrite('color_chart36.png', color_chart36)cv2.imwrite('color_chart52.png', color_chart52)cv2.imwrite('color_chart256.png', color_chart256)

渐变色带

# -*- coding: utf-8 -*-
import cv2
import numpy as npdef generate_color_band(left_colors, right_colors, grade=256, height=32):"""Generate color bands by uniformly changing from left colors to rightcolors. Note that there might be multiple bands.Parameters----------left_colors: Left colors of the color bands.right_colors: Right colors of the color bands.grade: how many colors are contained in one color band.height: height of one color band."""# check and process color parameters, which should be 2D list# after processingif not isinstance(left_colors, (tuple, list)):left_colors = [left_colors]if not isinstance(right_colors, (tuple, list)):right_colors = [right_colors]if not isinstance(left_colors[0], (tuple, list)):left_colors = [left_colors]if not isinstance(right_colors[0], (tuple, list)):right_colors = [right_colors]# initialize channel, and all other colors should have the same channelchannel = len(left_colors[0])band_num = len(left_colors)result = []for i in range(band_num):left_color = left_colors[i]right_color = right_colors[i]if len(left_color) != channel or len(right_color) != channel:raise ValueError("All colors should have same channel number")color_band = np.linspace(left_color, right_color, grade)color_band = np.expand_dims(color_band, axis=0)color_band = np.repeat(color_band, repeats=height, axis=0)color_band = np.clip(np.round(color_band), 0, 255).astype(np.uint8)result.append(color_band)result = np.concatenate(result, axis=0)result = np.squeeze(result)return resultif __name__ == '__main__':black = [0, 0, 0]white = [255, 255, 255]red = [0, 0, 255]green = [0, 255, 0]blue = [255, 0, 0]gray_band = generate_color_band([[0], [255]], [[255], [0]])color_band8 = generate_color_band([black, white, red, green, blue, black, black, black],[white, black, white, white, white, red, green, blue])cv2.imwrite('gray_band.png', gray_band)cv2.imwrite('color_band8.png', color_band8)

使用python生成颜色表(color chart)相关推荐

  1. python渐变颜色表_使用Python绘制颜色条渐变图+修改比例大小+修改渐变颜色,画,colorbar,刻度...

    Draw Gradient Color Map using python Dependencies pandas matplotlib numpy seaborn You can configure ...

  2. python渐变颜色表_python – 具有固定颜色渐变的np.histogram2D

    我正在尝试修改现有的 python代码,使用np.histogram2d绘制值的热图.我正在绘制其中的几个,我希望y轴和颜色范围在它们之间具有可比性.我找到了手动设置y_limit的方法,但现在我想要 ...

  3. Python matplotpy颜色表(python画图常用颜色)

    参考来源: python画图常用颜色_guoxinian的专栏-CSDN博客_python颜色

  4. Python matplotpy颜色表

  5. python rgb颜色表_RGB颜色对照表

    window系统下,简单的FTP上传和下载操作 先假设有一FTP服务器,FTP服务器:qint.ithot.net,用户名:username   密码:user1234.在本地电脑D:盘创建一个文件夹 ...

  6. 颜色表大全 | HTML Color Table

    颜色表大全 | HTML Color Table 以下是颜色表大全 ,可以按Ctrl+F快速查找需要的颜色 鸨色 #f7acbc 赤白橡 #deab8a 油色 #817936 绀桔梗 #444693 ...

  7. 就有趣,Python生成字符视频

    Python生成字符视频 一.前言 在之前也写过生成字符视频的文章,但是使用的是命令行窗口输出,效果不是很好,而且存在卡顿的情况.于是我打算直接生成一个mp4的字符视频.大致思路和之前一样:Pytho ...

  8. html十六进制和RGB颜色表

    自己做一个十六进制或者rgb的表,用在js里选颜色, 下表只是用于预览各种着色. 网上搜出的好多基本都没法用. 中文名 英文名 代码 RGB 预览 栗色 maroon #800000 128,0,0 ...

  9. java hsv_RGB与HSV之间的转换公式及颜色表

    RGB & HSV 英文全称 RGB - Red, Green, Blue HSV - Hue, Saturation, Value HSV --> RGB 转换公式 HSV --> ...

最新文章

  1. 归并排序是稳定的排序
  2. python高并发编程_python_day10_并发编程
  3. 【已解决】烂泥:耳机有声音,话筒却没有输入……
  4. 全球第一家只接收BCH的慈善组织
  5. 基于并联SVM支持向量机训练HOG特征提取的人员目标提取
  6. python自动源码_谷歌推出Tangent开源库,在Python源代码上做自动微分
  7. python学习格式化输出(一)
  8. 哪些设计模式最值得学习
  9. 极大似然估计的渐进正态性
  10. Git-简单安装与使用
  11. c语言学习-自定义函数并调用求1-100的累计和
  12. 庆祝.Net BI团队成立!
  13. DFS HDOJ 2181 哈密顿绕行世界问题
  14. 【Python】读取 txt 文件
  15. 易筋SpringBoot2.1 | 第二篇:Spring Boot配置文件详解
  16. 使用idea在serviceImpl中配置radis
  17. HTML制作课表源代码
  18. SqlDbx连接Mysql中文乱码
  19. Android之Fragment回退栈详解
  20. 铝网初效过滤器及金属网过滤器的区别

热门文章

  1. GPS从入门到放弃(十一) --- 差分GPS
  2. android 蓝牙 编程,Android编程之蓝牙测试实例
  3. 数据库系统及应用(SQL)
  4. [Golang]尾递归优化?
  5. Android 获取手机IMEI 和 IMSI 号
  6. C语言是从什么位置开始执行程序,c程序执行过程是从哪开始到哪里结束的
  7. 基于Docker的CaaS容器云平台架构设计及市场分析
  8. 计算机网络——PPP协议与HDLC协议
  9. 我的男朋友是个GAY
  10. chrome浏览器模拟鼠标点击插件clicker