利用Python實(shí)現(xiàn)生成顏色表(color chart)
前言
在做色彩相關(guān)的算法分析時(shí)候,經(jīng)常需要使用規(guī)則的顏色表來(lái)進(jìn)行輔助。下面用python(numpy和opencv)來(lái)生成顏色表并保存為圖片。
有兩種類型:
- 格子形狀的顏色表
- 漸變色帶
長(zhǎng)的樣子分別如下:
格子顏色表
這里需要注意,當(dāng)劃分的顏色數(shù)量比較少時(shí),最好把一個(gè)顏色像素?cái)U(kuò)展成為一個(gè)格子,不然的話整個(gè)圖看起來(lái)就太小了。
# -*- coding: utf-8 -*- import cv2 import numpy as np def generate_color_chart(block_num=18, block_columns=6, grid_width=32, grid_height=None): """ Generate color chart by uniformly distributed color indexes, only support 8 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 by block_num / block_columns grid_width: Width of color grid grid_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 sizes block_rows = np.int_(np.ceil(block_num / block_columns)) block_width = grid_width * block_num block_height = grid_height * block_num width = block_width * block_columns height = block_height * block_rows result = 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 channel for 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 index block_row = i // block_columns block_column = i % block_columns xmin = block_column * block_width ymin = block_row * block_height xmax = xmin + block_width ymax = ymin + block_height result[ymin:ymax, xmin:xmax, :] = color_block result = result[..., ::-1] # convert from rgb to bgr return result def expand_pixel_to_grid(matrix, grid_width, grid_height): """ Expand a pixel to a grid. Inside the grid, every pixel have the same value as the source pixel. Parameters ---------- matrix: 2D numpy array grid_width: width of grid grid_height: height of grid """ height, width = matrix.shape[:2] new_heigt = height * grid_height new_width = width * grid_width repeat_num = grid_width * grid_height matrix = 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 matrix if __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 np def generate_color_band(left_colors, right_colors, grade=256, height=32): """ Generate color bands by uniformly changing from left colors to right colors. 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 processing if 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 channel channel = 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 result if __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)
到此這篇關(guān)于利用Python實(shí)現(xiàn)生成顏色表(color chart)的文章就介紹到這了,更多相關(guān)Python顏色表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python入門(mén)前的第一課 python怎樣入門(mén)
人工智能這么火,0基礎(chǔ)能學(xué)python嗎?python該怎么選擇編輯器?怎么搭建python運(yùn)行環(huán)境?python好學(xué)嗎,怎么學(xué)?這是所有python入門(mén)前同學(xué)都會(huì)提出的疑問(wèn),這篇文章和大家一起學(xué)習(xí)python,感興趣的小伙伴們可以加入2018-03-03在python里創(chuàng)建一個(gè)任務(wù)(Task)實(shí)例
這篇文章主要介紹了在python里創(chuàng)建一個(gè)任務(wù)(Task)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04使用python根據(jù)端口號(hào)關(guān)閉進(jìn)程的方法
今天小編就為大家分享一篇使用python根據(jù)端口號(hào)關(guān)閉進(jìn)程的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11Python運(yùn)算符的應(yīng)用超全面詳細(xì)教程
Python運(yùn)算符是為了實(shí)現(xiàn)數(shù)值或字符運(yùn)算的特殊符號(hào)。Python運(yùn)算符可以分為算術(shù)運(yùn)算符、邏輯運(yùn)算符、賦值運(yùn)算符、成員運(yùn)算符、身份運(yùn)算符、比較運(yùn)算符、三目運(yùn)算符等。接下來(lái),我們就開(kāi)始來(lái)學(xué)習(xí)這一堆符號(hào)吧2022-07-07Python如何獲取實(shí)時(shí)股票信息的方法示例
本文主要介紹了Python如何獲取實(shí)時(shí)股票信息的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06python打印當(dāng)前文件的絕對(duì)路徑并解決打印為空的問(wèn)題
這篇文章主要介紹了python打印當(dāng)前文件的絕對(duì)路徑并解決打印為空的問(wèn)題,文中補(bǔ)充介紹了python中對(duì)文件路徑的獲取方法,需要的朋友可以參考下2023-03-03python打印異常信息的兩種實(shí)現(xiàn)方式
今天小編就為大家分享一篇python打印異常信息的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-1278行Python代碼實(shí)現(xiàn)現(xiàn)微信撤回消息功能
這篇文章主要介紹了78行Python代碼實(shí)現(xiàn)現(xiàn)微信撤回消息功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07