python numpy matplotlib画小方块填充背景色和添加不同色彩的文字

需要根据不同的数值生成不同色彩的小方块,并标明数值,考虑到各种色彩的方块,如果采用文字采用黑色或者白色总是避免不了和背景色重复,所以把数值转化成rgb(0x11,0xf0,0x3b)类似的值,然后文字采用rgb(255-0x11,255-0xf0,255-0x3b),要把背景色的rgb转成十进制再进行减法操作,这里为了方便陈述,用的十六进制没有转。
这里的filename为需要做方块图的数值,按一定格式存在此文件中例如:
11&330&a346&5789&
44&c3&20&11
按自己数值存放的格式解析就好了,总之得到需要作图的数值,按一定的方式转成rgb后再作图,或者自己挑选几种、几十种类似这样的#0012ce色彩值对应自己的数值也行,

import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import os
import cv2
import re #按固定长度截取字符串
img_output_path='/home/xxx/image/'
class draw_sequences():
    def __init__(self, idx):
        #背景颜色为黑色的画布
        #im = np.zeros((300, 300, 3), dtype="uint8") #3
        #背景颜色为白色的画布`
        self.im = np.ones((800,600,3),np.uint8)*255
        #画布使用默认字体
        self.font = cv2.FONT_HERSHEY_SIMPLEX
        #起始作图坐标
        self.leftx = 10
        self.lefty = 10
        #矩形单元长高
        self.len = 70
        self.high = 15
        self.filepath = img_output_path + '/' + idx
        #第idx张画布(也许一张不够)
        self.idx = idx
        self.bdraw = False
        self.filename = filename
    #画一个矩形序列单元
    def draw_sequence(self, listseq):
        if self.leftx + self.len > 600:
            self.leftx = 10
            self.move_down()
        self.bdraw = True
        #矩形边框颜色
        #cv2.rectangle(self.im, (self.leftx, self.lefty), (self.leftx+self.len, self.lefty+self.high), (int(listseq[0],16),int(listseq[1],16),int(listseq[2],16))) #12
        #填充矩形颜色
        rseq = int(listseq[0],16)
        gseq = (int(listseq[1],16)+100)%255
        bseq = (int(listseq[2],16)+50)%255
        cv2.rectangle(self.im, (self.leftx, self.lefty), (self.leftx+self.len, self.lefty+self.high), (rseq, gseq, bseq), thickness=-1)
        #填充文字
        seq = "&".join(str(i) for i in listseq)
        cv2.putText(self.im,seq,(self.leftx, self.lefty+10), self.font, 0.4, (255-rseq, 255-gseq,255-bseq), 1)
        self.move_right()

    #保存序列图
    def write_jpg(self):
        if self.bdraw == True:
            cv2.imwrite(self.filepath + ".jpg", self.im)
            self.idx = self.idx + 1
            self.__init__(self.idx)

    #往右移一个位置画序列单元
    def move_down(self):
        self.lefty = self.lefty + self.high
        self.leftx = 10
        if self.lefty >= 800:
            self.write_jpg()
    #另起一行画序列单元
    def move_right(self):
        self.leftx = self.leftx+self.len

cdraw = draw_sequences("", filemd5, 0)
txtread = open(filename,"r")
for line in txtread:
	sub_seqs = i.split('&')
	sub_seqs.pop()
	idx = 0
	lseq = []
	for i in sub_seqs:
		if len(i) > 2:
                    subseqs = re.findall(r'.{2}', i)
                    for j in subseqs:
                        lseq.append(j)
                        if (idx+1)%3 == 0:
                            cdraw.draw_sequence(lseq)
                            lseq = []
                        idx = idx+1
                else:
                    lseq.append(i)
                    if (idx+1)%3 == 0:
                        cdraw.draw_sequence(lseq)
                        lseq = []
                    idx = idx+1

            listlen = len(lseq)
            if (listlen):
                if listlen < 3:
                    for i in range(listlen,3):
                        lseq.append('00')
                cdraw.draw_sequence(lseq)
            cdraw.move_right()
cdraw.write_jpg()
del cdraw

这里是一行数值作一张图,&符号分割后的数字两位两位划分,不够的用0补,每6位数字作一个方块
python numpy matplotlib画小方块填充背景色和添加不同色彩的文字_第1张图片

你可能感兴趣的:(python)