日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

python怎么给画布填上颜色_python numpy matplotlib画小方块填充背景色和添加不同色彩的文字...

發(fā)布時(shí)間:2024/7/5 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python怎么给画布填上颜色_python numpy matplotlib画小方块填充背景色和添加不同色彩的文字... 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

需要根據(jù)不同的數(shù)值生成不同色彩的小方塊,并標(biāo)明數(shù)值,考慮到各種色彩的方塊,如果采用文字采用黑色或者白色總是避免不了和背景色重復(fù),所以把數(shù)值轉(zhuǎn)化成rgb(0x11,0xf0,0x3b)類似的值,然后文字采用rgb(255-0x11,255-0xf0,255-0x3b),要把背景色的rgb轉(zhuǎn)成十進(jìn)制再進(jìn)行減法操作,這里為了方便陳述,用的十六進(jìn)制沒(méi)有轉(zhuǎn)。

這里的filename為需要做方塊圖的數(shù)值,按一定格式存在此文件中例如:

11&330&a346&5789&

44&c3&20&11

按自己數(shù)值存放的格式解析就好了,總之得到需要作圖的數(shù)值,按一定的方式轉(zhuǎn)成rgb后再作圖,或者自己挑選幾種、幾十種類似這樣的#0012ce色彩值對(duì)應(yīng)自己的數(shù)值也行,

import numpy as np

import matplotlib

matplotlib.use('Agg')

import matplotlib.pyplot as plt

import os

import cv2

import re #按固定長(zhǎng)度截取字符串

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

#畫布使用默認(rèn)字體

self.font = cv2.FONT_HERSHEY_SIMPLEX

#起始作圖坐標(biāo)

self.leftx = 10

self.lefty = 10

#矩形單元長(zhǎng)高

self.len = 70

self.high = 15

self.filepath = img_output_path + '/' + idx

#第idx張畫布(也許一張不夠)

self.idx = idx

self.bdraw = False

self.filename = filename

#畫一個(gè)矩形序列單元

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)

#往右移一個(gè)位置畫序列單元

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

這里是一行數(shù)值作一張圖,&符號(hào)分割后的數(shù)字兩位兩位劃分,不夠的用0補(bǔ),每6位數(shù)字作一個(gè)方塊

總結(jié)

以上是生活随笔為你收集整理的python怎么给画布填上颜色_python numpy matplotlib画小方块填充背景色和添加不同色彩的文字...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。