01:将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果

【图像处理】

类似于图中效果:

py 2.7代码:

from PIL import Image, ImageDraw, ImageFont
def add_word(img):
char_size = 30
fillcolor = "#ff0000"
draw = ImageDraw.Draw(img)
my_font = ImageFont.truetype(r'C:\Windows\Fonts\SIMYOU.TTF', char_size)#从本地载入字体文件
width, height = img.size
draw.text((width - char_size,char_size-20), '', font=my_font, fill=fillcolor)
img.save('result.jpg','JPEG')
del draw if __name__ == "__main__":
img = Image.open('test.jpg')
add_word(img)

更多:

draw.line((0, 0) + im.size, fill=128)  #画一道线

参考文档:

pillow 函数接口查询 官方文档


02:任一个英文的纯文本文件,统计其中的单词出现的个数【文本处理】

import re

def statis_words(article):
re_pat = re.compile("\W",re.S)
pre_article = re.sub(re_pat," ",article)
re_pat2 = re.compile(" *",re.S)
list_words = re_pat2.split(pre_article)
dict_re = dict.fromkeys(list_words)
for i in list_words:
if not dict_re[i]:
dict_re[i] = 0
if i in list_words:
dict_re[i]+=1
for i in dict_re.iteritems():#打印
print i if __name__ == "__main__":
file_path = "words.txt"
article = ""
with open(file_path) as f:
for i in f.readlines():
article += i
statis_words(article.replace("\n",' '))

03:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小【图像处理】

import os
from PIL import Image iPhone5_WIDTH = 1136
iPhone5_HEIGHT = 640 def resize_iPhone5_pic(path, new_path, width=iPhone5_WIDTH, height=iPhone5_HEIGHT):
im = Image.open(path)
w,h = im.size if w > width:
h = width * h // w
w = width
if h > height:
w = height * w // h
h = height im_resized = im.resize((w,h), Image.ANTIALIAS)
im_resized.save(new_path) def walk_dir_and_resize(path):
for root, dirs, files in os.walk(path):#递归path下所有目录
for f_name in files:
if f_name.lower().endswith('jpg'):
path_dst = os.path.join(root,f_name)
f_new_name = 'iPhone5_' + f_name
resize_iPhone5_pic(path=path_dst, new_path=f_new_name) if __name__ == '__main__':
walk_dir_and_resize('./')#当前目录

核心函数  image.resize()

Image.resize(sizeresample=0)

Returns a resized copy of this image.

Parameters:
  • size – The requested size in pixels, as a 2-tuple: (width, height).
  • resample – An optional resampling filter. This can be one of PIL.Image.NEAREST (use nearest neighbour), PIL.Image.BILINEAR (linear interpolation), PIL.Image.BICUBIC(cubic spline interpolation), or PIL.Image.LANCZOS (a high-quality downsampling filter). If omitted, or if the image has mode “1” or “P”, it is set PIL.Image.NEAREST.
Returns:

An Image object.

size: 图像宽度,长度

resample:

PIL.Image.NEAREST (use nearest neighbour)   最近邻插值法

PIL.Image.BILINEAR (linear interpolation),   双线性插值法

PIL.Image.BICUBIC(cubic spline interpolation), 双三次插值

or PIL.Image.LANCZOS (a high-quality downsampling filter)   Lanczos算法  采样放缩算法

缩小时 ANTIALIAS

更多图像处理请参考 opencv

reference:Image Module


04:你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词【字符串处理】【文件管理】

注:暂认为出现频率最多的为最重要的

#文件管理
#coding:utf-8
import os
import re def analyse_article(article):
re_pat = re.compile("(?=[\n\x21-\x7e]+)[^A-Za-z0-9]")#+|[{}【】。,;“‘”?]")#("^([\u4e00-\u9fa5]+|[a-zA-Z0-9]+)$")#("(?=[\x21-\x7e]+)[^A-Za-z0-9]+|["{}【】。,;’“‘”?"]")#("[\W\u4e00-\u9fa5] ",re.S) \s 空格符
pre_article = re.sub(re_pat," ",article)
chinese_symbol = ["\xa1\xa3","\xa1\xb0","\xa1\xb1","\xa3\xac","\xa1\xbe","\xa1\xbf","\xa1\xb6","\xa1\xb7","\xa3\xba","\xa3\xbb"]#中文标点
for i in chinese_symbol:
pre_article = pre_article.replace(i," ")
re_pat2 = re.compile(" *",re.S)
list_words = re_pat2.split(pre_article)
dict_re = dict.fromkeys(list_words)
#print pre_article
for i in list_words:
if not dict_re[i]:
dict_re[i] = 0
if i in list_words:
dict_re[i]+=1
if dict_re.get(""):
del dict_re[""]
key_words = sorted(dict_re.items(),key = lambda e:e[1])[-1]
return (key_words[0], key_words[1]) def walk_dir_and_analyse(path):
text = ""
key_words_list = []
for root, dirs, files in os.walk(path):#递归path下所有目录
for f_name in files:
if f_name.lower().endswith('txt'):
with open(os.path.join(root,f_name)) as f:
for i in f.readlines():
text += i
key_words_list.append(analyse_article(text)) for i in key_words_list:
print "\""+ i[0] + "\" for "+ str(i[1]) +" times" if __name__ == "__main__":
walk_dir_and_analyse("./")

输出

>python 4.py
"春眠不觉晓" for 2 times


05:敏感词文本文件 filtered_words.txt,当用户输入敏感词语,则用星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市」。

#[字符串处理]
#敏感词文本文件 filtered_words.txt,
#里面的内容为以下内容,当用户输入敏感词语时,
#python ' '中将自动加入结尾符号,要注意字串实际长度,包括读入txt文件时的字符串长度
#coding:utf-8 def words_filter(path,words_list):
content = ""
with open(path) as f:
for i in f.readlines():
for j in words_list:
if j in i:
i = i.replace(j,"*"*(len(j)/(len('单')-1))) #一个中文两个字节长度
content += i
return content if __name__ == "__main__":
word_path = "filtered_words.txt"
path = "words.txt" words_list = []
with open(word_path) as f:
for i in f.readlines():
words_list.append(i.replace("\n",""))
print words_filter(path,words_list)

Python 练习册的更多相关文章

  1. Python练习册--PIL处理图片之加水印

    背景 最近在看到了Python 练习册,每天一个小程序 这个项目,非常有趣,也比较实用. 晚上看了这第000题,关于Python图片处理: 将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似 ...

  2. Python 练习册--生成唯一激活码(邀请码)

    题目是这样子的: 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? 分析 其实要生成 ...

  3. Python 练习册,每天一个小程序----第0000题

    题目 第 0000 题: 将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果. 类似于图中效果 Code: from PIL import Image,Imag ...

  4. python练习册0004题

    在任意一个英文文档中,统计单词出现的次数, 分析: 本题不是很难,单词通常以空格隔开,但是有些单词后面跟一些特殊符号,只需把这些特殊符号替换掉就可以了, 代码一 import re file_name ...

  5. 【python练习册】1.3 将1.2题生成的n个激活码保存到mysql关系型数据库中

    该题涉及到mysql中一些指令,先熟悉一下 MySQL指令 参考:https://www.cnblogs.com/zhuyongzhe/p/7686105.html mysql -u root -p ...

  6. python练习册 每天一个小程序 第0013题

    # -*-coding:utf-8-*- ''' 题目描述: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-) 地址: http://tieba.baidu.com/p/21 ...

  7. python练习册 每天一个小程序 第0010题

    # -*-coding:utf-8-*- ''' 题目描述: 使用 Python 生成类似于下图中的字母验证码图片 思路: 运用PIL库加random 随机字母进行生成 ''' import rand ...

  8. python练习册 每天一个小程序 第0001题

    1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生 ...

  9. python练习册 每天一个小程序 第0000题

    PIL库学习链接:http://blog.csdn.net/column/details/pythonpil.html?&page=1 1 #-*-coding:utf-8-*- 2 __au ...

随机推荐

  1. 使用TCMalloc的堆栈检查

    在前一篇译文<TCMalloc:线程缓冲的Malloc>详细讲解了TCMalloc的工作原理和特点,今天翻译<heap-checking using tcmalloc>,了解T ...

  2. java报表工具finereport常用函数的用法总结(数组函数)

    ADD2ARRAY ADDARRAY(array,insertArray, start):在数组第start个位置插入insertArray中的所有元素,再返回该数组. 示例: ADDARRAY([3 ...

  3. 时光煮雨 Unity3D实现2D人物移动-总结篇

    系列目录 [Unity3D基础]让物体动起来①--基于UGUI的鼠标点击移动 [Unity3D基础]让物体动起来②--UGUI鼠标点击逐帧移动 时光煮雨 Unity3D让物体动起来③—UGUI DoT ...

  4. NOIP2013积木大赛

    题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成 ...

  5. 有关数据库行、锁 的几个问题(rowlock)

    行锁的基本说明: SELECT au_lname FROM authors WITH (NOLOCK) 锁定提示                                 描述  HOLDLOC ...

  6. [No00000D]word如何批量删除超链接 怎么去掉网址保留文字

    1.删除超链接的文字及网址 这种情况是想把带有网址的文字统统删掉,文字和网址一概不留. 首先在word界面按下ALT+F9(在所有的域代码及其结果间进行切换.),超链接文本会被转换成代码的样式. 例如 ...

  7. EF的入门使用 (电影管理)

    控制器代码: public class HomeController : Controller { private NewDBContext ndc = new NewDBContext(); pub ...

  8. 关于maya动力学系统的一些总结

    maya动力学有以下几套系统: 1.刚体.柔体系统 刚体系统的典型节点连接方法如下: 物体的变换节点.形状节点连接rigidBody节点,刚体节点输出力到解算器节点,解算器输出新的变换到变换节点 值得 ...

  9. Android平台的一些常用命令

    一.Android常见目录结构 1.  apk文件在/data/app目录下 2.  /data/data/[package.name]目录,存放程序数据缓存等等 3.SD卡安装方式的,则在/mnt/ ...

  10. Centos5.8 安装 PHP5.5 和 memcached

    安装GIT 需要先安装gcc-c++ (sudo yum install gcc-c++) sudo yum install gettext-devel expat-devel cpio perl o ...