0、将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。 类似于图中效果
# coding=utf-8
from PIL import Image, ImageDraw, ImageFont # Pillow
# 打开原图片
openImg = Image.open('./etc/ac345982b2b7d0a2e416b6c2caef76094a369a96.jpg')
size = (0, 0, 125, 125)
# 新图片
newImg = Image.new('RGB', openImg.size, (0, 0, 0))
draw = ImageDraw.Draw(newImg)
# 粘贴
newImg.paste(openImg.crop(size), # 复制
size) # 大小
draw.text((90, 2), # 坐标
'14', # 字符串
font = ImageFont.truetype('./etc/arial.ttf', 24), # 字体
fill = (255, 0, 0)) # 颜色
newImg.save('./etc/code1.png', 'png')
参考:
http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00140767171357714f87a053a824ffd811d98a83b58ec13000
http://liam0205.me/2015/04/22/pil-tutorial-basic-usage/
1、做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?
# coding=utf-8
import random
from PIL import Image, ImageDraw, ImageFont # Pillow
# 字符串相关
_String = 'Q W E R T Y U I O P A S D F G H J K L Z X C V B N M q w e r t y u i o p a s d f g h j k l z x c v b n m 1 2 3 4 5 6 7 8 9 0'.split(' ')
_Length = len(_String) - 1
# 生成随机字符串(优惠券id)
# @param {number} length: 字符串长度
# @return {string}: 返回字符串
def randomString(length = 0):
s = ''
i = 0
while i < length:
s += _String[random.randint(0, _Length)]
i += 1
return s
# 生成优惠券
# @param {string} stringID: 优惠券id
_Font = ImageFont.truetype('./etc/arial.ttf', 24)
def youHuiQuan(stringID):
img = Image.new('RGB', (200, 40), (0, 0, 0))
draw = ImageDraw.Draw(img)
draw.text((5, 8), stringID, font = _Font, fill = (255, 255, 255))
img.save('./result/' + stringID + '.jpg', 'jpeg')
# 初始化
def init():
i = 0
j = 5
while i < j:
t = randomString(10)
youHuiQuan(t)
i += 1
init()
2、将 1 题生成的激活码(或者优惠券)保存到 MySQL关系型数据库中。
# coding=utf-8
import random
import pymysql
from PIL import Image, ImageDraw, ImageFont # Pillow
# 字符串相关
_String = 'Q W E R T Y U I O P A S D F G H J K L Z X C V B N M q w e r t y u i o p a s d f g h j k l z x c v b n m 1 2 3 4 5 6 7 8 9 0'.split(' ')
_Length = len(_String) - 1
# 生成随机字符串(优惠券id)
# @param {number} length: 字符串长度
# @return {string}: 返回字符串
def randomString(length = 0):
s = ''
i = 0
while i < length:
s += _String[random.randint(0, _Length)]
i += 1
return s
# 生成优惠券
# @param {string} stringID: 优惠券id
_Font = ImageFont.truetype('./etc/arial.ttf', 24)
def youHuiQuan(stringID):
img = Image.new('RGB', (200, 40), (0, 0, 0))
draw = ImageDraw.Draw(img)
draw.text((5, 8), stringID, font = _Font, fill = (255, 255, 255))
img.save('./result/' + stringID + '.jpg', 'jpeg')
# 存入数据库
# @param db: 数据库
# @param cursor: 数据库游标
# @param {string} rs: 随机字符串
# @return {number}: 返回数据库是否有该字符串的结果
def sql(db, cursor, rs):
# 判断数据库是否有该字符串
sql1 = '''SELECT str FROM random
WHERE str = '%s' ''' % (rs)
r1 = cursor.execute(sql1)
# 如果r1 = 0,表示该字符串没有生成过
if r1 == 0:
# 将字符串插入到数据库中
sql2 = '''INSERT INTO random
(str)
VALUES ('%s')''' % (rs)
r2 = cursor.execute(sql2)
db.commit()
return r1
# 初始化
'''
CREATE TABLE if not exists `random` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`str` varchar(255) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
'''
def init():
i = 0
j = 4
db = pymysql.connect('localhost', 'lianxi', 'lianxi', 'lianxi')
cursor = db.cursor()
# 循环
while i < j:
# 生成随机字符串并将结果存入到数据库
t = randomString(10)
n = sql(db, cursor, t)
# 返回0表示该随机字符串没有生成过,生成图片并使i+1
if n == 0:
youHuiQuan(t)
i += 1
db.close()
init()
3、任一个英文的纯文本文件,统计其中的单词出现的个数。
# coding=utf8
import re
# 打开文件并读取文本
file = open('./etc/t4.txt', 'r')
text = file.read()
file.close()
# 正则
regexp = re.compile(r'''[\s # 匹配空格
\n # 匹配换行
.,!():'"?<> # 匹配特殊符号
]+ # 匹配>=1个符合规则''', re.X)
newText = re.split(regexp, text)
# 统计每个单词出现的个数
# -1是因为数组的最后一位是''
wordLength = {}
for index in range(len(newText) - 1):
if newText[index] in wordLength:
wordLength[newText[index]] += 1
else:
wordLength[newText[index]] = 1
# 输出结果
print('A total of ' + str(len(newText) - 1) + ' words appeared.\n')
for key in wordLength:
print(key + ':' + str(wordLength[key]))
4、用 Python 写一个爬图片的程序,爬这个链接里的妹子图片 :)
# coding=utf8
import re
import urllib
import urllib2
_TieBaURL = 'http://tieba.baidu.com/p/2166231880'
# get请求
def get(url):
request = urllib2.Request(url)
response = urllib2.urlopen(request)
return response.read()
# 获取图片字符串
def getImgUrl(html):
h = re.compile(r'http://([^\s]+)\.(png|jpg)', re.I)
return h.findall(html)
# 下载
def download(urls):
txt = ''
for index in range(len(urls)):
t = urls[index][0] + '.' + urls[index][1] # 文件地址
u = 'http://' + t # http + 文件地址
urllib.urlretrieve(u, './image/' + str(index) + '.' + urls[index][1]) # 下载并保存
txt += u + '\n'
# 把地址存到文档里
file = open('./image/url.txt', 'w+')
file.write(txt)
file.close()
# 初始化
def init():
html = get(_TieBaURL) # 请求
urls = getImgUrl(html) # 分解获得地址
download(urls) # 下载
init()