python 玩耍天地
词云 wordcloud
wordcloud轮子下载
- 下载上面链接里对应的 whl (我选的 xxx_win32.whl)到 本地 进行安装
一般地, 会结合 jieba 分词 还有 matplotlib 来做
初识庐山面目
# -*- coding: utf-8 -*-
#导入所需库
from wordcloud import WordCloud
f = open(r'xxx\AI\go\wordcollections.txt','r').read()
wordcloud = WordCloud(background_color="white",width=1000, height=860, margin=2).generate(f)
__author__ = 'Frank Li'
# width,height,margin可以设置图片属性
# generate 可以对全部文本进行自动分词,但是对中文支持不好
# 可以设置font_path参数来设置字体集
#background_color参数为设置背景颜色,默认颜色为黑色import matplotlib.pyplot as plt
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
wordcloud.to_file('test.png')
# 保存图片,但是在第三模块的例子中 图片大小将会按照 mask 保存# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from PIL import Image as image
import jieba
import numpy
from wordcloud import WordCloud, ImageColorGenerator, STOPWORDS__author__ = 'Frank Li'def chinese_jieba(text):# 通过jieba工具将中文文本做处理,并返回指定格式的内容wordlist_jieba = jieba.cut(text)text_jieba = ' '.join(wordlist_jieba)return text_jiebadef main(filename):with open(filename, 'rb') as f:text = chinese_jieba(f.read().decode())mask_pic = numpy.array(image.open('heart.png')) # 打开图像处理,设置遮罩层# 设置固定的词个数为0# stopwords = {'黑娃':0,'白嘉轩':0}# 也可以按照下面格式设置stopwords = set(STOPWORDS)stopwords = stopwords.union(set(['黑娃', '白嘉轩'])) # 将不想在词云上出现的词,放入集合中,也就是设置固定词个数为0wordclod = WordCloud(background_color='white', # 设置背景颜色,默认是黑色margin=0,max_words=2000, # 关键字的个数max_font_size=100, # 字体大小font_path=r'C:\Windows\Fonts\simsun.ttc', # 设置中文识别mask=mask_pic, # 添加遮罩层,也就是设置词云形状stopwords=stopwords, # 过滤固定的词).generate(text) # 将text里面所有的词统计,产生词云# image_colors = ImageColorGenerator(mask_pic)plt.imshow(wordclod)# plt.imshow(wordclod.recolor(color_func=image_colors))plt.axis('off')plt.show()wordclod.to_file('bailuyuan.jpg') # 保存图片if __name__ == "__main__":filename = r'C:\Users\FrankLi\Downloads\白鹿原.txt'main(filename)
将图像转为 字符画
# -*- coding: utf-8 -*-
__author__ = 'Frank Li'
import os
import math
from PIL import Imageclass ProcessBar(object):"""一个打印进度条的类"""def __init__(self, total): # 初始化传入总数self.shape = ['▏', '▎', '▍', '▋', '▊', '▉']self.shape_num = len(self.shape)self.row_num = 30self.now = 0self.total = totaldef print_next(self, now=-1): # 默认+1if now == -1:self.now += 1else:self.now = nowrate = math.ceil((self.now / self.total) * (self.row_num * self.shape_num))head = rate // self.shape_numtail = rate % self.shape_numinfo = self.shape[-1] * headif tail != 0:info += self.shape[tail-1]full_info = '[%s%s] [%.2f%%]' % (info, (self.row_num-len(info)) * ' ', 100 * self.now / self.total)print("\r", end='', flush=True)print(full_info, end='', flush=True)if self.now == self.total:print('')class MosaicImg(object):'''用于将源图像当成一个像素点合成目标图像'''def __init__(self,target, # 目标图像的路径,图像的透明区域会被忽略img_path, # 源图片的目录,里面的图片将被拿来作为像素点pixel_size=50, # 每个像素图片的宽高size=(0, 0), # 最后生成的图片的尺寸,格式是 (width, height)scale_factor=-1, # 缩放因子,0~1之间的浮点数,如果设定了这个,则 size 无效):path = os.path.split(target)name, ext = os.path.splitext(path[1])self.save_img = os.path.join(path[0], "mosaic_" + name + '.png') # 生成的图像名字self.pixel_size = pixel_size# 处理目标图像self.target = Image.open(target).convert('RGBA')if scale_factor == -1: # 使用 size 参数self.target.thumbnail(size)else: # 使用 scale_factor 参数new_size = (scale_factor * self.target.size[0], scale_factor * self.target.size[1])self.target.thumbnail(new_size)# 处理源图像self.images = {} # key是一张图片的主颜色,value是该图片的 PIL.Image 对象self.images_count = {}file_list = os.listdir(img_path)print("正在读取源图像:")pb = ProcessBar(len(file_list))for file in file_list: # 遍历目录img = Image.open(os.path.join(img_path, file)).convert('RGB')img.thumbnail((pixel_size, pixel_size)) # 重设图片大小color = self.main_color(img) # 计算主颜色self.images[color] = imgself.images_count[color] = 0pb.print_next()# 每个图像最多使用的次数self.max_img_use = 3 * self.target.size[0] * self.target.size[1] / len(self.images)def gen_mosaic_image(self):'''使用初始化的设置生成像素图。其具体做法是遍历目标图像,对每个像素点的颜色,从源图像的主颜色中找到一个最接近的,然后将该图像当成像素点'''# 最后生成的图像的大小size = (self.pixel_size * self.target.size[0], self.pixel_size * self.target.size[1])self.mosaic_img = Image.new('RGBA', size)# 开始生成图像print("正在生成图像: ")# 遍历每一个像素pb = ProcessBar(self.target.size[0])for x in range(self.target.size[0]):for y in range(self.target.size[1]):r, g, b, a = self.target.getpixel((x, y)) # 得到该像素的颜色if a == 0: # 跳过透明区域continuemin_score = 1000min_color = Nonefor color in self.images: # 找到最接近的颜色score = self.color_distance((r, g, b), color)if score < min_score:min_score = scoremin_color = color# 将图片贴上去self.mosaic_img.paste(self.images[min_color], (x*self.pixel_size, y*self.pixel_size))self.images_count[min_color] += 1 # 使用次数+1# 超过了最大使用次数就删除if self.images_count[min_color] > self.max_img_use:self.images.pop(min_color)pb.print_next()print('正在保存图像,请稍候。')self.mosaic_img.save(self.save_img, format='PNG')return self.mosaic_imgdef main_color(self, image):'''得到一张图片的主颜色,这里使用了偷懒的做法,将图片resize到一个像素点大小,取该点的像素值'''img = image.resize((1, 1), Image.BICUBIC)return img.getpixel((0, 0))def color_distance(self, rgb_1, rgb_2):'''两个RGB颜色的相似度计算,使用LAB颜色空间'''R_1, G_1, B_1 = rgb_1R_2, G_2, B_2 = rgb_2rmean = (R_1 + R_2) / 2R = R_1 - R_2G = G_1 - G_2B = B_1 - B_2return math.sqrt((2+rmean/256)*(R**2)+4*(G**2)+(2+(255-rmean)/256)*(B**2))if __name__ == '__main__':current_path = os.path.dirname(os.path.abspath(__file__)) # 当前目录的完整路径img_path = os.path.join(current_path, 'images/') # 存放源图像的目录target = os.path.join(current_path, 'images/xinxin.png') # 目标图像ma = MosaicImg(target, img_path, 70, scale_factor=0.1) # 初始化img = ma.gen_mosaic_image()
短信接口 twilio
scrapy + fake_useragent 爬虫
# 做事就要做极致,要学便学无上法,最佳实践 ,不管他多难
pip 指定源
pip install pillow -i http://mirrors.aliyun.com/pypi/simple/ --extra-index-url https://pypi.python.org/simple --trusted-host mirrors.aliyun.com清华:https://pypi.tuna.tsinghua.edu.cn/simple阿里云:http://mirrors.aliyun.com/pypi/simple/中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/华中理工大学:http://pypi.hustunique.com/山东理工大学:http://pypi.sdutlinux.org/ 豆瓣:http://pypi.douban.com/simple/
DOS 时代游戏
DOS 游戏有些无聊
https://dos.zczc.cz/
转载于:https://www.cnblogs.com/Frank99/p/10549168.html
python 玩耍天地相关推荐
- windows上在python玩耍深度学习资源合集
文章目录 一.windows上安装python版OpenCV 1.换源 2.升级pip 3.安装 二.安装GPU版pytorch 一.windows上安装python版OpenCV 首先要安装各种py ...
- Python玩耍:一个小小的爬虫,在一堆公司列表里筛选出总部位于中国的公司
这周接了个小活,发个博客庆祝一下 需要从https://www.mwcbarcelona.com/exhibition/2019-exhibitors/这个网站上将所有的公司照下来(24000+),并 ...
- vscode 搜索结果 整行_如何用VSCode愉快的写Python
在学习Python的过程中,一直没有找到比较趁手的第三方编辑器,用的最多的还是Python自带的编辑器.由于本人用惯了宇宙第一IDE(Visual Studio),所以当Visual Studio C ...
- python免费入门教程-python小白如何入门,第一步要做什么?(附带免费教程)
Python小白一枚,不知道如何入门,上网整理了一些点,简单记录一下,希望对接下来的学习有帮助吧. 1.找到合适的入门书籍,大致读一次,搞懂(太难的跳过). 2.做些简单习题,字符串比较,读取日期之类 ...
- python 合并 循环list_阿里巴巴鼎力推荐,Python入门至精通,24招加速你的Python
对于Python初学者想更轻松的学好Python开发,Python爬虫技术,Python数据分析,人工智能等技术,这里也给大家准备了一套Python系统教学资源,下面介绍下阿里巴巴推荐的大型Pytho ...
- 如何在 Windows 上安装 Python | Linux 中国
本文字数:4184,阅读时长大约:6分钟 导读:安装 Python,启动 IDE,然后你就可以在 Windows 系统下进行编程了. https://linux.cn/article-12335-1. ...
- python编程第四版_清华编程教授强力推荐《Python编程》,指导你如何学习python
Python编程真的那么容易吗?仅仅是看理论就可以学以致用吗? 今天我给你介绍的这本书,也许会让你开始改变这种想法,因为这本书上的练习和案例以及指导本身就足够学好Python了. 清华编程教授强力推荐 ...
- python学生名片系统_Python入门教程完整版400集(懂中文就能学会)快来带走
如何入门Python?权威Python大型400集视频,学了Python可以做什么? 小编今天给大家分享一套高老师的python400集视频教程,里面包含入门进阶,源码,实战项目等等,,不管你是正在学 ...
- python樱花制作教程视频_大型Python视频资料,阿里巴巴推荐,用Python画一棵漂亮的樱花树...
原标题:大型Python视频资料,阿里巴巴推荐,用Python画一棵漂亮的樱花树 对于初学者想更轻松的学好Python开发,爬虫技术,Python数据分析,人工智能等技术,这里也给大家准备了一套系统教 ...
最新文章
- mapreduce工作流程
- 一起谈.NET技术,也玩MVC3.0 Razor自定义视图引擎来修改默认的Views目录结构
- js中的arguments 参数
- VTK:InfoVis之DelimitedTextWriter
- 道理我都懂,但你到底为什么偏偏喜欢咬我??
- 自然语言处理中的语言模型与预训练技术的总结
- chm editor
- 将一个数组中不重复_你不知道的解法:数组中重复的数字
- linux mutex 数量上限,互斥量mutex
- A Simple RESTful API Service With Node.js And Koa2
- elasticsearch的javaAPI之index
- Windows Server 2016-DHCP服务器审核日志大小调整
- C语言贴图图片路径不对,backgroundImage 路径问题 vue 图片的引入方式
- 获取元素的位置信息的方法
- Q1营收超华尔街预期,挚文集团的价值等待回归
- k8s安装prometheus+grafana(第二弹:prometheus-operator)
- vuefilters过滤器的使用,给金钱价格加上符号单位
- 先打开计算机主机电源,开电脑为什么要先打开显示器,再开主机
- 【Win10系统右下角网络图标消失、无法联网、网络Internet状态闪退等问题解决方法】
- 软件安装【持续更新ing】