본문 바로가기

Python_Intermediate/WordCloud

WordCloud Font RGB Color

반응형

Code 1>

from wordcloud import WordCloud
from matplotlib import pyplot

def make_colors(word, font_size, position, orientation, random_state, **kwargs):
color = "rgb(0, 0, 0)"

return color

text = ''
with open("파일경로명", encoding="utf-8") as f:
text = f.read()

wc = WordCloud(width=1200, height=800, scale=2.0, max_font_size=150,
background_color="#ffffff")
gen = wc.generate(text)

recolor = gen.recolor(color_func=make_colors, random_state=True)

pyplot.figure()
pyplot.imshow(recolor, interpolation='bilinear')
wc.to_file("출력파일명")
pyplot.close()


Code 2>

from wordcloud import WordCloud
from matplotlib import pyplot

def make_colors(word, font_size, position, orientation, random_state, **kwargs):
r = random_state.randint(0, 255)
g = random_state.randint(0, 255)
b = random_state.randint(0, 255)

color = "rgb(%d, %d, %d)" % (r, g, b)

return color

text = ''
with open("파일경로명", encoding="utf-8") as f:
text = f.read()

wc = WordCloud(width=1200, height=800, scale=2.0, max_font_size=150,
background_color="#ffffff")
gen = wc.generate(text)

recolor = gen.recolor(color_func=make_colors, random_state=True)

pyplot.figure()
pyplot.imshow(recolor, interpolation='bilinear')
wc.to_file("출력파일명")
pyplot.close()


정리>

RGB는 빨강, 녹색, 파랑의 색상을 0 ~ 255로 숫자로 표현한 색상 코드 값이다.

Code1는 RGB를 지정하여 출력하는 코드이다. 

Code2는 RGB Color Code를 랜덤으로 받아 출력파일명을 만들때마다 다양한 색상을 지정할 수 있다.

0,0,0은 검정, 255, 255, 255는 흰색을 의미한다.

RGB Color Code는 https://www.w3schools.com/colors/colors_rgb.asp 을 참조하면 좋다.


예제출력물 Code 1>



예제출력물 Code 2>


반응형