├── .gitignore ├── License ├── README.md ├── data ├── BON VOYAGE.txt ├── Jungle P.txt ├── Run!Run!Run!.txt ├── Shining Ray.txt ├── believe.txt ├── free will.txt ├── memories.txt ├── share the world.txt ├── 全新世界.txt ├── 冒险世界.txt ├── 向著阳光.txt ├── 心的地图.txt ├── 未来航海.txt ├── 永久指针.txt └── 疯狂彩虹.txt ├── fonts └── simhei.ttf ├── gif_example.py ├── gif_example2.py ├── gifs ├── ace.gif └── one_piece │ ├── a.jpg │ ├── b.jpg │ ├── c.jpg │ ├── d.jpg │ ├── e.jpg │ ├── f.jpg │ ├── g.jpg │ └── h.jpg ├── images ├── luffy.png └── tony.png ├── luffy_result.png ├── output ├── ace.gif ├── happy.png ├── luffy.png ├── one_piece.gif ├── sad.png └── tony.png ├── qiaoba.png ├── requirements.txt ├── save_example.py ├── sentiment ├── BosonNLP_sentiment_score.txt ├── happy.png ├── license.txt ├── readme.txt └── sad.png ├── sentiment_analyser.py ├── sentiment_example.py ├── show_example.py ├── weibo ├── happy.txt └── sad.txt └── words_image.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | .DS_Store 3 | .idea/* 4 | -------------------------------------------------------------------------------- /License: -------------------------------------------------------------------------------- 1 | Copyright (C) 2016 flingjie 2 | 3 |   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 |    5 |   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 生成图形词云 2 | ========== 3 | 4 | 使用jieba和wordcloud生成图形词云. 5 | 其中情感分析部分使用了bosonnlp提供的情感词典[http://bosonnlp.com/dev/resource](http://bosonnlp.com/dev/resource),感谢. 6 | 7 | ## 安装 8 | 9 | pip install -r requirements.txt 10 | 11 | ## 使用 12 | 13 | 1.处理文本, 显示云图 14 | 15 | # content: 输入的文本内容 16 | # image_name: 图片完整路径 17 | show_image(content, image_name) 18 | 19 | 2.处理文本, 保存到文件夹 20 | 21 | # content: 输入的文本内容 22 | # image_name: 图片完整路径 23 | # output_dir: 结果输出的文件夹路径 24 | save_image(content, image_name, output_dir) 25 | 26 | 3.对文本进行情感分析, 根据结果输出不同图像 27 | 28 | # content: 29 | # sentiment: 情感分, 大于等于0 显示笑脸, 小于0显示哭脸 30 | show_emotion(content, sentiment) 31 | 32 | 4.处理文本, 生成gif 33 | 34 | # gif 生成 gif 35 | 36 | # content: 输入的文本内容 37 | # gif_name: gif图片完整路径 38 | # output_dir: 39 | # duration: 图片帧切换间隔时间 40 | words2gif(content, gif_name, output_dir, duration=0.5) 41 | 42 | # 多张静态图生成gif 43 | 44 | # content: 输入的文本内容 45 | # images_dir: 静态图文件夹 46 | # output_dir: 47 | # duration: 图片帧切换间隔时间 48 | words2gif_from_images(content, images_dir, output_dir, duration=0.5) 49 | 50 | ## 示例 51 | 52 | *简单显示* 53 | 54 | from words_image import get_keywords, show_image 55 | import os 56 | 57 | BASE_DIR = os.path.dirname(__file__) 58 | # 文本文件夹路径 59 | DATA_DIR = os.path.join(BASE_DIR, "data") 60 | # 图片文件夹路径 61 | IMAGE_DIR = os.path.join(BASE_DIR, "images") 62 | # 源图片路径 63 | # 其中图片中白色的部分会被忽略 64 | IMAGE_PATH = os.path.join(IMAGE_DIR, "tony.png") 65 | 66 | if __name__ == "__main__": 67 | words = get_keywords(DATA_DIR) 68 | # 显示生成的词图 69 | show_image(words, IMAGE_PATH) 70 | 71 | *批量处理并保存* 72 | 73 | from words_image import get_keywords, save_image 74 | import os 75 | 76 | BASE_DIR = os.path.dirname(__file__) 77 | # 文本文件夹路径 78 | DATA_DIR = os.path.join(BASE_DIR, "data") 79 | # 图片文件夹路径 80 | IMAGE_DIR = os.path.join(BASE_DIR, "images") 81 | # 结果保存路径 82 | OUTPUT_DIR = os.path.join(BASE_DIR, "output") 83 | 84 | if __name__ == "__main__": 85 | words = get_keywords(DATA_DIR) 86 | for image in os.listdir(IMAGE_DIR): 87 | image_name = os.path.join(IMAGE_DIR, image) 88 | save_image(words, image_name, OUTPUT_DIR) 89 | 90 | 91 | *根据情感生成不同表情* 92 | 93 | BASE_DIR = os.path.dirname(__file__) 94 | WEIBO_DIR = os.path.join(BASE_DIR, 'weibo') 95 | # 博文路径 96 | WEIBO_PATH = os.path.join(WEIBO_DIR, 'happy.txt') 97 | 98 | 99 | if __name__ == "__main__": 100 | # 获取博文关键字列表和情感分数 101 | words, sentiment = get_words_sentiment(WEIBO_PATH) 102 | # 根据情感分数显示笑脸或悲伤 103 | show_emotion(words, sentiment) 104 | 105 | ## 截图 106 | 107 | *路飞* 108 | 109 | ![luffy](output/luffy.png) 110 | 111 | *乔巴* 112 | 113 | ![tony](output/tony.png) 114 | 115 | *动图* 116 | 117 | ![one piece](output/one_piece.gif) 118 | 119 | *笑脸* 120 | 121 | ![happy](output/happy.png) 122 | 123 | *伤感* 124 | 125 | ![sad](output/sad.png) 126 | 127 | ## 注意 128 | 129 | 默认安装的images2gif只支持PIL, 不支持Pillow, 130 | 需要将: 131 | 132 | for im in images: 133 | palettes.append( getheader(im)[1] ) 134 | 135 | 修改为: 136 | 137 | for im in images: 138 | palettes.append( im.palette.getdata()[1] ) 139 | -------------------------------------------------------------------------------- /data/BON VOYAGE.txt: -------------------------------------------------------------------------------- 1 | 知道吗?在大海的尽头 2 | 有一个巨大的宝藏 3 | 将它拿到手的人就能成为海贼王 4 | 是不是很令人兴奋? 5 | 从未听说过的冒险在等待着我们 6 | 7 | 一路顺风 鼓起那一点点的勇气 8 | 已经可以依稀看见那未来的征途 9 | 10 | 最初大家描绘的那些零乱的地平线 11 | 现在已经可以在一架望远镜中看到 12 | 那正在困扰你心灵的迷惑的命运之罗盘 13 | 却可以让一切顺畅地继续 14 | 15 | 一路顺风 16 | 将羁绊和过去全部丢弃 17 | 尽管如此我们还是应该尽情地笑 18 | 请不要吝惜那为了梦想而流下的泪水 19 | 它在我生命中最为珍贵 20 | 已经可以依稀看见那未来的征途了 -------------------------------------------------------------------------------- /data/Jungle P.txt: -------------------------------------------------------------------------------- 1 | 漫历世间汪洋 2 | 高声呐喊 3 | 怀着畅快的心情吹响出发的号角 4 | 5 | 避开命运安排的航线 6 | 我们向着天空与海洋的分界点 7 | 扬帆起航 8 | 在黑暗的大海深渊 展开令人屏息的冒险 9 | 单凭想像不就很令人期待吗? 10 | 11 | 漫历世间汪洋 12 | 高声呐喊 13 | 怀着畅快的心情吹响出发的号角 14 | 15 | 真想永远牢印 16 | 那出航时的心情 17 | 那样就能无所畏惧 18 | 如今无法抑制那高昂的情绪 踏上旅途吧 19 | 每个腼腆的内心都铭刻下冒险的旋律 20 | 21 | 分享着那宝藏般的喜悦 22 | 高声畅笑 23 | 在狂欢的喜宴中将它们放飞 24 | 随风飘扬 25 | 26 | Life is "Adventurous" 27 | Be aware it's "Dangerous" 28 | Who's gonna be "One of us" 29 | And a trip goes on because 30 | We da pirate of the "Mass" 31 | To the West To the East 32 | Gotta find my way, Sail away 33 | All the way to "One Piece" 34 | 35 | 将梦想填入空荡的胸膛 36 | 展翅高飞 37 | 当舒畅的风填满你的内心 38 | 勇往直前 39 | 40 | 分享着那宝藏般的喜悦 41 | 高声畅笑 42 | 在狂欢的喜宴中将它们放飞 43 | 随风飘扬 -------------------------------------------------------------------------------- /data/Run!Run!Run!.txt: -------------------------------------------------------------------------------- 1 | 因为传递不了满怀的心情 2 | 所以要紧紧握着你的手 3 | 就算是一个人我也要出发 4 | 再怎么远也请你一直看着我 5 | 6 | 今天早上我一直在想 7 | 为什么如此的投入呢 8 | 什么时候开始 9 | 心里老盘旋着这些话 10 | 因为想抓住开始起跑的心 11 | 我自己也只有跟着走了 12 | 13 | 我也想拥抱开始起飞的梦想 14 | 如果是和你一起 我就可以向前冲 -------------------------------------------------------------------------------- /data/Shining Ray.txt: -------------------------------------------------------------------------------- 1 | 要往哪里走 才能找到更加美好的明天 2 | 举起船舵 奇迹的地图就会在胸中展开 3 | 向着天空 寻找梦想 虽然也会迷路 4 | 但和你相遇绝对不会是梦想 5 | 寻找一个完美的自己 6 | 只要有一点点勇气 就能得到很大的收获 7 | 愿望如今已成为了誓言 8 | 9 | Shining Ray! Find you pure brand new way 10 | 憧憬着未来的发展 11 | 现在 让所有的想法迎风飞舞 12 | Shining Ray! Find you pure brand new way 13 | A never ending journey to be together 14 | 无论到哪里都会一直追逐 Shining Ray 15 | -------------------------------------------------------------------------------- /data/believe.txt: -------------------------------------------------------------------------------- 1 | 世代传承的意志 时代的浪潮 人的梦想 2 | 这些都是无法阻挡的 3 | 只要人们继续追求自由的解答 4 | 这一切都将永不停止 5 | 6 | 我只相信着未来 就算有人笑我也无所谓 7 | 奔驰的热情让你更耀眼 8 | 虽然好刺眼 但我仍要继续凝视 9 | 有种美学的感觉 10 | I'm really really stuck on you! 11 | 12 | 不同于任何人梦想的背影 13 | 追,不停的追 14 | 高潮迭起直到到手为止 15 | Believe In Wonderland! 16 | 17 | 那个别人都看不见的梦 18 | 抓,努力去抓 19 | 我会一路跟随 热切的心情 20 | 我不一定要合乎逻辑 21 | 那些一成不变的日子 22 | 朝天堂前进 23 | Believe In Wonderland! -------------------------------------------------------------------------------- /data/free will.txt: -------------------------------------------------------------------------------- 1 | 继续朝着没有尽头的天空前进 2 | 现在开始描述似曾相识的未来 3 | 有时自己也可能会失败 4 | 但是总会出现温暖的彼方 5 | 值得骄傲的 我自己拥有的地方 6 | 仿佛有什么在轻推我的背 7 | 在漫天的星空下 8 | 无法忘怀 -------------------------------------------------------------------------------- /data/memories.txt: -------------------------------------------------------------------------------- 1 | 小时候我的藏宝图 2 | 总是浮现在脑海中 3 | 永远在寻找奇迹之地 4 | 不输给远方的某人 5 | 6 | 啦啦啦… 现在的每一天 啦啦啦… 都充满尘埃 7 | 何时 啦啦啦… 所有的一切 啦啦啦… 8 | 都随时间而过 9 | 10 | 如果世界能够改变 11 | 对于一无所知的我 12 | 带我走吧,为了重拾童年的回忆 13 | 14 | 从小唱的歌 15 | 温暖着梦的心 16 | 大家都仿效过的秘密的旋律 17 | 这次更加动听 18 | 19 | 啦啦啦… 现在我 啦啦啦… 仍在叹息 20 | 每个人 啦啦啦… 都还没有 啦啦啦… 21 | 真正抓住梦想 22 | 23 | 如果时间能够倒流 24 | 带我回到初识泪水的那一刻 25 | 为了不再寂寞 26 | 27 | 如果世界能够改变 28 | 对于一无所知的我 29 | 带我走吧,为了重拾童年的回忆 30 | 31 | 带我走吧 为了不再寂寞 32 | -------------------------------------------------------------------------------- /data/share the world.txt: -------------------------------------------------------------------------------- 1 | 为了拥抱 2 | 那片长空 3 | I bielive 4 | 奔向同一个明天 5 | hey~ 6 | 7 | come on let's go everybody oh we share the music 8 | 9 | come on let's go everybody oh we share the one dream 10 | 11 | come on let's go everybody oh we share the good times 12 | 13 | come on let's go everybody oh we share the one world 14 | 15 | 在迷失的 16 | 黑暗之中 17 | 摸索谜题 18 | 19 | 当我们失去方向 20 | 无所适从之时 21 | 22 | umm you and me yes 23 | 畅快 24 | 在这个同甘共苦的世界上 25 | 26 | 引导你我 27 | I feel the beat 28 | 踏上旅途 29 | oh yeah 30 | 31 | share the music 32 | 无论何时 33 | 34 | share the one dream 35 | 相互信赖 36 | 37 | share the good times 38 | 手牵着手 39 | 40 | share the one world now 41 | 42 | 为了拥抱 43 | 那片长空 44 | 谁也阻挡不了我的脚步 45 | 46 | 排除万难 47 | I bielieve 48 | 奔向同一个明天 49 | yeah yeah 50 | 51 | come on let's go everybody oh we share the music 52 | 53 | come on let's go everybody oh we share the one dream 54 | 55 | 你追我赶 56 | 梅比斯环 57 | 上的角逐 58 | 59 | 相识 60 | 乐观 61 | 渴望 62 | share style 63 | 64 | 起始 蔓延 这片超乎想象的flavor 65 | 66 | 全身 I feel so good 享受自由 oh yeah 67 | 68 | share the music 无论多远 69 | 70 | share the one dream 相互倾诉 71 | 72 | share the good times 开怀畅笑 73 | 74 | share the one world now 75 | 76 | 为了拥抱那片长空 谁也阻挡不了我的脚步 77 | 78 | 排除万难 I bielieve 奔向同一个明天 yeah yeah 79 | 80 | 在那堵无形的墙后 有你在等待着我 81 | 82 | 这就去相会 I bielieve 一同奔向新世界yeah yeah 83 | 84 | come on let's go everybody oh we share the music 85 | 86 | come on let's go everybody oh we share the one world 87 | -------------------------------------------------------------------------------- /data/全新世界.txt: -------------------------------------------------------------------------------- 1 | 向前冲 向前冲 2 | 向天空高高扬起旗帜 3 | 跨越无边无际的大海探求其中神秘 4 | 寻找大家从未见过的 5 | 世界奇迹 6 | 7 | 怀着跃动的心情 随心所欲 8 | 向前进 9 | 沿着追梦之人留下的足迹 10 | 就算是暴风雨的夜晚 11 | 只要齐心协力 12 | 就定能冲破险峻 13 | 无论何时 14 | 15 | 向前冲 向前冲 16 | 向天空高高扬起旗帜 17 | 虽然也会流下不愿示人的泪水 18 | 追赶着 追赶着 19 | 指示梦想的罗盘 20 | 直率的眼神 21 | 我们不会止步 22 | Around the world 23 | Start me up 24 | 25 | 向前冲 向前冲 26 | 冲得比谁都前 27 | 若无法逃脱 28 | 就紧紧抓住明天 29 | 追赶着 追赶着 30 | 如今在眼中映出的一切 31 | 铭刻在纯洁的心中 32 | Brand new world -------------------------------------------------------------------------------- /data/冒险世界.txt: -------------------------------------------------------------------------------- 1 | 面对重重障碍更加勇敢面对 2 | 此刻怀着高涨的力量大步向前冲 3 | 无所畏惧 哪怕抛弃一切 4 | 最后定能绽开笑颜 5 | 6 | 用双手把握第一 目标自由自在的Wonderland 7 | 看啊 看那微弱的光芒照耀着明日之路 孤注一掷开创时代 8 | 永远充满力量Let's go 若要获得荣耀 9 | 一定就算此刻 无所畏惧 终能和同伴一起赢得 10 | 来吧 向着广阔的世界大步奔跑 11 | 怀着未知的力量飞向空中 12 | 没有畏惧 不再迷惘 纵然路途艰难 13 | 14 | 最后面对重重障碍定能更加勇敢面对 15 | 此刻怀着高涨的力量大步向前冲 16 | 无所畏惧 哪怕抛弃一切 17 | 也要勇往直前 18 | 奔向明天 19 | 最后定能绽开笑颜 -------------------------------------------------------------------------------- /data/向著阳光.txt: -------------------------------------------------------------------------------- 1 | 世界吗 2 | 没错 3 | 去追求自由 4 | 这个能够选择的世界就在你的面前无限延伸着 5 | 未完成的梦想就是引导你们前进的方向 6 | 去超越吧 7 | 在信念的旗帜的带领下 8 | 9 | 我刚刚启程去寻找 10 | 伴随着飞溅的浪花 11 | 永不停息地奔向世界 12 | 心中的热情驱使着我 坚持到最后一刻 13 | 追寻那从未见过的曙光 14 | 15 | 夏日的太阳 鼓励心灵的风帆 16 | 开辟通往新世界的航海图 17 | 乘风破浪 冲出绝望 18 | 向水平线的那一端前进 19 | 20 | 我刚刚启程去寻找 21 | 伴随着飞溅的浪花 22 | 永不停息地奔向世界 23 | 心中的热情驱使着我 坚持到最后一刻 24 | 追寻那从未见过的曙光 -------------------------------------------------------------------------------- /data/心的地图.txt: -------------------------------------------------------------------------------- 1 | 不要怕,来吧,我们一起向前迈进 2 | 太阳永在我们心中 3 | 我们手牵手传递力量 4 | 紧紧抓住那希望 5 | 6 | 我们乘风破浪,齐心协力 7 | 向那振奋人心的地方疾速前航,信号已经响起 8 | 现在正是起航的时候,收起船锚 9 | 迎着那七色彩风,驶向冒险之海 10 | 虽然前路坎坷 11 | 但那里有珍贵的宝藏 12 | 即使前路凶险,无论何时 13 | 我都会保护着你 14 | 15 | 不要怕,来吧,我们一起向前迈进 16 | 太阳永在我们心中 17 | 暴风雨来了,我们肩并肩 18 | 遥望前方的希望 19 | 集起梦的碎片 20 | 就能成为迈向未来的航海图 21 | 让我们在同一面旗帜下 22 | 紧紧抓住那希望 23 | 我们是一条心的好伙伴 24 | -------------------------------------------------------------------------------- /data/未来航海.txt: -------------------------------------------------------------------------------- 1 | 向着橙色的遥远地平线 2 | 今日的风拂过 3 | 渡过黎明之海 4 | 夜不能寐 眼中只有明日 5 | 这等热情 任谁也无法阻挠 6 | 希望也好 传说也罢 7 | 虽然经历万千 8 | 总之背负一切 微笑就好 9 | 无论何时我们都拥有梦想 10 | 力争上游 11 | 高高伸出双臂 12 | 无论何时都永存心中 13 | 拥抱那太阳 14 | 一定能到达 15 | 那个地方 -------------------------------------------------------------------------------- /data/永久指针.txt: -------------------------------------------------------------------------------- 1 | 紧紧抓住梦的碎片 2 | 我们追逐着不变的目标 3 | 漂浮在那憧憬的天空中 4 | 精疲力竭之时仍紧握罗盘 5 | 不断地追寻着梦想 6 | 7 | 何时才能到达梦的彼岸 8 | 就这样尽快成熟起来 9 | 抬头仰望浩瀚的天空 10 | 直到永久 永久 11 | 12 | 儿时梦的故事 13 | 追忆已忘却的那模糊不清的年代 14 | 至今也不曾熄灭胸中的火种 15 | 向那熟悉的万里晴空 16 | 寄托朋友和孩子的思念 17 | 越过时代的变迁 18 | 直到永久 永久 -------------------------------------------------------------------------------- /data/疯狂彩虹.txt: -------------------------------------------------------------------------------- 1 | Darling Darling 2 | 蔚蓝色的大海上浮动的云彩 3 | 沾湿了迷茫的明天 4 | 黄昏来临前也要尽情欢笑 5 | 用希望在小小的心灵中 6 | 刻下梦想的咒文 7 | 8 | 疯狂的梦幻之星!! 9 | 耀眼的梦幻之星!! 10 | 七色的音符奏出加速的预感 11 | 疯狂的梦幻之星!! 12 | 不一起来吗? 13 | 我们堕天使在释放梦想 14 | Darling Darling 15 | 16 | 疯狂的梦幻之星!! 17 | 我是一颗疯狂的梦幻之星!! 18 | 疯狂的梦幻之星!! 19 | 耀眼的梦幻之星!! 20 | 蔚蓝色的大海将叠上怎样的色彩? 21 | 疯狂的梦幻之星!! 22 | 耀眼的梦幻之星!! 23 | 将眼中的悲伤化去 24 | 送你一根温暖的羽毛 25 | 不可思议的堕天使来自心底的歌唱 26 | Darling Darling 27 | -------------------------------------------------------------------------------- /fonts/simhei.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/fonts/simhei.ttf -------------------------------------------------------------------------------- /gif_example.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from words_image import get_keywords, words2gif 3 | import os 4 | 5 | BASE_DIR = os.path.dirname(__file__) 6 | DATA_DIR = os.path.join(BASE_DIR, "data") 7 | GIF_DIR = os.path.join(BASE_DIR, "gifs") 8 | GIF_PATH = os.path.join(GIF_DIR, "ace.gif") 9 | OUTPUT_DIR = os.path.join(BASE_DIR, "output") 10 | 11 | 12 | if __name__ == "__main__": 13 | words = get_keywords(DATA_DIR) 14 | words2gif(words, GIF_PATH, OUTPUT_DIR) 15 | -------------------------------------------------------------------------------- /gif_example2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from words_image import get_keywords, words2gif_from_images 3 | import os 4 | 5 | BASE_DIR = os.path.dirname(__file__) 6 | DATA_DIR = os.path.join(BASE_DIR, "data") 7 | GIF_DIR = os.path.join(BASE_DIR, "gifs") 8 | IMAGE_PATH = os.path.join(GIF_DIR, "one_piece") 9 | OUTPUT_DIR = os.path.join(BASE_DIR, "output") 10 | 11 | 12 | if __name__ == "__main__": 13 | words = get_keywords(DATA_DIR) 14 | words2gif_from_images(words, IMAGE_PATH, OUTPUT_DIR) 15 | -------------------------------------------------------------------------------- /gifs/ace.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/gifs/ace.gif -------------------------------------------------------------------------------- /gifs/one_piece/a.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/gifs/one_piece/a.jpg -------------------------------------------------------------------------------- /gifs/one_piece/b.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/gifs/one_piece/b.jpg -------------------------------------------------------------------------------- /gifs/one_piece/c.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/gifs/one_piece/c.jpg -------------------------------------------------------------------------------- /gifs/one_piece/d.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/gifs/one_piece/d.jpg -------------------------------------------------------------------------------- /gifs/one_piece/e.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/gifs/one_piece/e.jpg -------------------------------------------------------------------------------- /gifs/one_piece/f.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/gifs/one_piece/f.jpg -------------------------------------------------------------------------------- /gifs/one_piece/g.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/gifs/one_piece/g.jpg -------------------------------------------------------------------------------- /gifs/one_piece/h.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/gifs/one_piece/h.jpg -------------------------------------------------------------------------------- /images/luffy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/images/luffy.png -------------------------------------------------------------------------------- /images/tony.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/images/tony.png -------------------------------------------------------------------------------- /luffy_result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/luffy_result.png -------------------------------------------------------------------------------- /output/ace.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/output/ace.gif -------------------------------------------------------------------------------- /output/happy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/output/happy.png -------------------------------------------------------------------------------- /output/luffy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/output/luffy.png -------------------------------------------------------------------------------- /output/one_piece.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/output/one_piece.gif -------------------------------------------------------------------------------- /output/sad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/output/sad.png -------------------------------------------------------------------------------- /output/tony.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/output/tony.png -------------------------------------------------------------------------------- /qiaoba.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/qiaoba.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | appnope==0.1.0 2 | cycler==0.9.0 3 | decorator==4.0.6 4 | gnureadline==6.3.3 5 | images2gif==1.0.1 6 | ipython==4.0.1 7 | ipython-genutils==0.1.0 8 | jieba==0.38 9 | matplotlib==1.5.0 10 | numpy==1.10.4 11 | path.py==8.1.2 12 | pexpect==4.0.1 13 | pickleshare==0.5 14 | Pillow==3.1.0 15 | ptyprocess==0.5 16 | pyparsing==2.0.7 17 | python-dateutil==2.4.2 18 | pytz==2015.7 19 | simplegeneric==0.8.1 20 | six==1.10.0 21 | snownlp==0.12.3 22 | traitlets==4.0.0 23 | wheel==0.24.0 24 | wordcloud==1.2 25 | -------------------------------------------------------------------------------- /save_example.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from words_image import get_keywords, save_image 3 | import os 4 | 5 | BASE_DIR = os.path.dirname(__file__) 6 | DATA_DIR = os.path.join(BASE_DIR, "data") 7 | IMAGE_DIR = os.path.join(BASE_DIR, "images") 8 | OUTPUT_DIR = os.path.join(BASE_DIR, "output") 9 | 10 | if __name__ == "__main__": 11 | words = get_keywords(DATA_DIR) 12 | for image in os.listdir(IMAGE_DIR): 13 | image_name = os.path.join(IMAGE_DIR, image) 14 | save_image(words, image_name, OUTPUT_DIR) 15 | -------------------------------------------------------------------------------- /sentiment/happy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/sentiment/happy.png -------------------------------------------------------------------------------- /sentiment/license.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/sentiment/license.txt -------------------------------------------------------------------------------- /sentiment/readme.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/sentiment/readme.txt -------------------------------------------------------------------------------- /sentiment/sad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/flingjie/words_image/e1c966919ead9313f9f570d39dea4dc8c7647db9/sentiment/sad.png -------------------------------------------------------------------------------- /sentiment_analyser.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import os 3 | import jieba 4 | 5 | BASE_DIR = os.path.dirname(__file__) 6 | TRAINING_DATA_DIR = os.path.join(BASE_DIR, 'sentiment') 7 | SENTIMENT_FILE = os.path.join(TRAINING_DATA_DIR, 'BosonNLP_sentiment_score.txt') 8 | BASE_NUMBER = 8 9 | WORDS_NUMBER = 100 10 | 11 | 12 | def load_data(): 13 | sentiment_data = {} 14 | with open(SENTIMENT_FILE, 'rb') as f: 15 | for line in f.readlines(): 16 | if ' ' not in line: 17 | continue 18 | word, score = line.split(' ') 19 | sentiment_data[word.strip()] = float(score.strip()) 20 | return sentiment_data 21 | 22 | 23 | def get_words_sentiment(filename): 24 | sentiment_data = load_data() 25 | content = open(filename, 'rb').read() 26 | words = jieba.cut(content) 27 | sentiment = 0 28 | result = [] 29 | for w in words: 30 | sentiment += sentiment_data.get(w, 0) 31 | result.append((w, int(sentiment + BASE_NUMBER))) 32 | factor = int(WORDS_NUMBER / len(result)) 33 | result *= factor 34 | return result, sentiment 35 | 36 | 37 | -------------------------------------------------------------------------------- /sentiment_example.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import os 3 | from sentiment_analyser import get_words_sentiment 4 | from words_image import show_emotion 5 | 6 | BASE_DIR = os.path.dirname(__file__) 7 | WEIBO_DIR = os.path.join(BASE_DIR, 'weibo') 8 | WEIBO_PATH = os.path.join(WEIBO_DIR, 'happy.txt') 9 | 10 | 11 | if __name__ == "__main__": 12 | words, sentiment = get_words_sentiment(WEIBO_PATH) 13 | show_emotion(words, sentiment) 14 | 15 | 16 | -------------------------------------------------------------------------------- /show_example.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from words_image import get_keywords, show_image 3 | import os 4 | 5 | BASE_DIR = os.path.dirname(__file__) 6 | DATA_DIR = os.path.join(BASE_DIR, "data") 7 | IMAGE_DIR = os.path.join(BASE_DIR, "images") 8 | IMAGE_PATH = os.path.join(IMAGE_DIR, "tony.png") 9 | 10 | if __name__ == "__main__": 11 | words = get_keywords(DATA_DIR) 12 | show_image(words, IMAGE_PATH) 13 | -------------------------------------------------------------------------------- /weibo/happy.txt: -------------------------------------------------------------------------------- 1 | 我做了个伟大的决定, 我今后一定要...就超帅!超酷!超有范!超多小女生追!的那种, 哈哈 -------------------------------------------------------------------------------- /weibo/sad.txt: -------------------------------------------------------------------------------- 1 | 卧槽!这种XX头衔的!这么一副语气倒卖?呵呵!我也真无语!那XX和赚钱的人真多啊!我胆子就大!不好友圈这种行为真特么作呕! -------------------------------------------------------------------------------- /words_image.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import jieba.analyse 3 | from PIL import Image, ImageSequence 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from wordcloud import WordCloud, ImageColorGenerator 7 | import os 8 | from images2gif import writeGif 9 | 10 | 11 | MAX_WORDS = 1000 12 | CUR_DIR = os.path.dirname(__file__) 13 | SENTIMENT_DIR = os.path.join(CUR_DIR, "sentiment") 14 | HAPPY_PATH = os.path.join(SENTIMENT_DIR, 'happy.png') 15 | SAD_PATH = os.path.join(SENTIMENT_DIR, 'sad.png') 16 | TEMP_DIR = os.path.join(CUR_DIR, 'tmp') 17 | 18 | 19 | def get_keywords(data_dir): 20 | content = '' 21 | for f in os.listdir(data_dir): 22 | filename = os.path.join(data_dir, f) 23 | if os.path.isfile(filename): 24 | content += open(filename, 'rb').read() 25 | content += '\n' 26 | words = jieba.analyse.textrank(content, topK=MAX_WORDS, withWeight=True) 27 | return words 28 | 29 | 30 | def generate_image(words, image): 31 | graph = np.array(image) 32 | wc = WordCloud(font_path=os.path.join(CUR_DIR, 'fonts/simhei.ttf'), 33 | background_color='white', max_words=MAX_WORDS, mask=graph) 34 | wc.generate_from_frequencies(words) 35 | image_color = ImageColorGenerator(graph) 36 | return wc, image_color 37 | 38 | 39 | def show_image(content, image_name): 40 | image = Image.open(image_name) 41 | wc, image_color = generate_image(content, image) 42 | plt.imshow(wc) 43 | plt.axis("off") 44 | plt.imshow(wc.recolor(color_func=image_color)) 45 | plt.axis("off") 46 | plt.show() 47 | 48 | 49 | def save_image(content, image_name, output_dir): 50 | image = Image.open(image_name) 51 | wc, image_color = generate_image(content, image) 52 | plt.imshow(wc) 53 | plt.axis("off") 54 | plt.imshow(wc.recolor(color_func=image_color)) 55 | plt.axis("off") 56 | output_path = os.path.join(output_dir, os.path.basename(image_name)) 57 | plt.savefig(output_path) 58 | 59 | 60 | def show_emotion(content, sentiment): 61 | if sentiment < 0: 62 | image = Image.open(SAD_PATH) 63 | else: 64 | image = Image.open(HAPPY_PATH) 65 | wc, image_color = generate_image(content, image) 66 | plt.imshow(wc) 67 | plt.axis("off") 68 | plt.imshow(wc.recolor(color_func=image_color)) 69 | plt.axis("off") 70 | plt.show() 71 | 72 | 73 | def words2gif(content, gif_name, output_dir, duration=0.5): 74 | im = Image.open(gif_name) 75 | palette = im.getpalette() 76 | i = 0 77 | images = [] 78 | try: 79 | while True: 80 | print("Start handle frame {}".format(i+1)) 81 | im.putpalette(palette) 82 | new_im = Image.new('RGB', im.size, (255, 255, 255)) 83 | new_im.paste(im) 84 | wc, image_color = generate_image(content, new_im) 85 | wc.recolor(color_func=image_color) 86 | images.append(wc.to_image()) 87 | i += 1 88 | im.seek(im.tell() + 1) 89 | except EOFError: 90 | print("Process end") 91 | print("Start generate gif ... ") 92 | out_path = os.path.join(output_dir, os.path.basename(gif_name)) 93 | writeGif(out_path, images, duration=duration) 94 | 95 | 96 | def words2gif_from_images(content, images_dir, output_dir, duration=0.5): 97 | images = [] 98 | for f in os.listdir(images_dir): 99 | filename = os.path.join(images_dir, f) 100 | if os.path.isfile(filename): 101 | print("Start handle {}".format(f)) 102 | image = Image.open(filename) 103 | wc, image_color = generate_image(content, image) 104 | wc.recolor(color_func=image_color) 105 | images.append(wc.to_image()) 106 | out_path = os.path.join(output_dir, "{}.gif".format(os.path.basename(images_dir))) 107 | writeGif(out_path, images, duration=duration) 108 | --------------------------------------------------------------------------------