Pythonを使ってかっこいい画像を作成してみる①

word cloud

pythonを使って慣れてきたら、かっこいい画像を作ってみませんか?初心者でも簡単に汎用性の高い画像を作れるようになります。

ゴール目標

ちなみに、中の英文は好き勝手に変えられます。添付はAviciiの有名な「waiting for love」 の歌詞を埋め込みました。

おすすめする人

難易度★☆☆☆☆
時間★☆☆☆☆
汎用性★★☆☆☆

環境

Mac10.14
python3.7

手順

モジュールのインストール

必要なモジュールをインストールします。

$pip install wordcloud

【word cloud】文章中で出現頻度が高い単語を複数選び出し、その頻度に応じた大きさで図示する手法。 ウェブページやブログなどに頻出する単語を自動的に並べることなどを指す。 文字の大きさだけでなく、色、字体、向きに変化をつけることで、文章の内容をひと目で印象づけることができる。

実行

from wordcloud import WordCloud #単語の出現頻度の可視化

text = "Where there's a will, there's a way, kind of beautiful And every night has its day, so magical And if there's love in this life, there's no obstacle That can't be defeated For every tyrant a tear for the vulnerable In every lost soul the bones of a miracle For every dreamer a dream we're unstoppable With something to believe in Monday left me broken Tuesday I was through with hopingWednesday my empty arms were open Thursday waiting for love, waiting for love Thank the stars it's Friday I'm burning like a fire gone wild on Saturday Guess I won't be coming to church on Sunday I'll be waiting for love, waiting for love To come around We are one of a kind irreplaceable How did I get so blind and so cynical If there's love in this life we're unstoppable No, we can't be defeated"

wordcloud = WordCloud(width = 480 , height = 320) #サイズ設定
wordcloud.generate(text) #テキスト読み込み
wordcloud.to_file("wordcloud.png") #生成した画像の保存 ""内は保存するファイル名

では上記のコードを実行してみましょう。プログラムが実行された場所にファイル画像が作成されます。

こんな感じです

では次に、text=””を変えてみましょう。例えば、Alan walkerの「faded」の歌詞を入れてみます。

Faded/Alan walker
勉強中さん
かっこいい画像を作成できました!
でも日本語のかっこいい画像は作れないんでしょうか?背景も黒以外に変えてみたいです。

次回は日本を使用した画像の作成方法をお教えします。

コメントを残す

メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です