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

# -*-coding:utf-8-*-

‘‘‘
题目描述:
  使用 Python 生成类似于下图中的字母验证码图片

思路:
  运用PIL库加random 随机字母进行生成

‘‘‘

import random
import string
from PIL import Image, ImageDraw, ImageFont, ImageFilter

def rnword():
    return random.choice(string.letters)

def color():
    return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))

def color2():
    return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))

def draw(width, height, n):
    bgcolor = (255, 2155, 255)
    image = Image.new(‘RGB‘, (width, height), bgcolor)
    font = ImageFont.truetype(‘C:/windows/fonts/Arial.ttf‘, 30)
    fontcolor = (0, 0, 0)
    draw = ImageDraw.Draw(image)

    for x in range(width):
        for y in range(height):
            draw.point((x, y), fill=color())

    for w in range(n):
        draw.text((60 * w + 10, 10), rnword(), font=font, fill=color2())

    image = image.filter(ImageFilter.BLUR)
    image.save(‘test3.jpg‘, ‘jpeg‘)

if __name__ == ‘__main__‘:
    draw(240, 60, 4)
时间: 2024-08-05 02:53:14

python练习册 每天一个小程序 第0010题的相关文章

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

# -*-coding:utf-8-*- ''' 题目描述: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-) 地址: http://tieba.baidu.com/p/2166231880 思路: 用正则表达式匹配图片链接,然后进行下载 ''' ''' import re import requests def main(): url = 'http://tieba.baidu.com/p/2166231880' response = requests.get(url

python练习册 每天一个小程序 第0009题

1 ''' 2 题目描述: 3 找出一个html文件中所有的url 4 5 思路 : 6 利用正则表达式进行匹配 7 8 ''' 9 10 11 import re 12 13 14 with open('test.txt') as fp: 15 text = fp.read() 16 pattern = re.compile( 17 "((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\

python练习册 每天一个小程序 第0004题

1 #-*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述:任一个英文的纯文本文件,统计其中的单词出现的个数. 5 参考学习链接: 6 re http://www.cnblogs.com/tina-python/p/5508402.html#undefined 7 collections http://blog.csdn.net/liufang0001/article/details/54618484 8 ''' 9 import re,co

python练习册 每天一个小程序 第0002题

1 #-*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中. 6 ''' 7 """ 8 import MySQLdb as mdb 9 10 config = { 11 'host': '127.0.0.1', 12 'port': 3306, 13 'user': 'root', 14 'passwd': '', 15 'd

python练习册 每天一个小程序

PIL库学习链接:http://blog.csdn.net/column/details/pythonpil.html?&page=1 1 #-*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目说明: 5 将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果. 类似于图中效果 6 ''' 7 from PIL import Image 8 from PIL import ImageChops 9 from P

Python 练习册,每天一个小程序

Python 练习册,每天一个小程序 说明: Python 练习册,每天一个小程序.注:将 Python 换成其他语言,大多数题目也适用 不会出现诸如「打印九九乘法表」.「打印水仙花」之类的题目 点此链接,会看到每个题目的代码, 欢迎大家 Pull Request 出题目,贴代码(Gist.Blog皆可):-) 本文本文由@史江歌([email protected] QQ:499065469)根据互联网资料收集整理而成,感谢互联网,感谢各位的分享.鸣谢!本文会不断更新. Talk is chea

Python练习册,每天一个小程序

Python练习册,每天一个小程序 精选评论关注该公众号可参与评论 写评论 加载中 以上评论由公众帐号筛选后显示 Python练习册,每天一个小程序 提交 我的评论 已评论 Python练习册,每天一个小程序 2014-12-15 程序猿 说明: ●Python 练习册,每天一个小程序.注:将 Python 换成其他语言,大多数题目也试用 ●不会出现诸如「打印九九乘法表」.「打印水仙花」之类的题目 ●欢迎大家 Pull Request 出题目,贴代码(Gist.Blog皆可):-) ●访问链接h

Python 练习冊,每天一个小程序

Python 练习冊,每天一个小程序 说明: Github 原文地址: 点击打开链接 Python 练习冊.每天一个小程序.注:将 Python 换成其它语言,大多数题目也试用 不会出现诸如「打印九九乘法表」.「打印水仙花」之类的题目 欢迎大家 Pull Request 出题目.贴代码(Gist.Blog皆可):-) Talk is cheap. Show me the code.--Linus Torvalds 第 0000 题:将你的 QQ 头像(或者微博头像)右上角加上红色的数字.类似于微

python实现简单的数学小程序

冒泡法排列list利用打标记来优化 避免重复循环杨辉三角形process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=" alt="python实现简单的数学小程序" />建立二维列表,if判定打印出第一个与最后一个1,其余利用列表索引计算与上图类似将每一行列表尾部补0,利用负索引 直接