韩漫免费漫画在线观看方法,《好好疼爱里面》免费看,年轻漂亮的女士护士内衣,妈妈醉酒后把我当爸爸电视剧

文章 > Python高级 > 手把手教你用Python PIL模块随机生成中文验证码

手把手教你用Python PIL模块随机生成中文验证码

头像

silencement

2019-07-19 10:54:104987浏览 · 0收藏 · 0评论

PIL是Python Imaging Library的简称,PIL是一个Python处理图片的库,提供了一系列模块和方法,比如:裁切,平移,旋转,改变尺寸等等。已经是Python平台事实上的图像处理标准库了。PIL功能非常强大,但API却非常简单易用。

PIL有如下几个模块:Image模块、ImageChops模块、ImageCrackCode模块、ImageDraw模块、ImageEnhance模块、ImageFile模块、ImageFileIO模块、ImageFilter模块、ImageFont模块、ImageGrab模块、ImageOps模块、ImagePath模块、ImageSequence模块、ImageStat模块、ImageTk模块、ImageWin模块、PSDraw模块。

在PIL库中,任何一个图像都是用Image对象来表示的,所以要加载一张图片,最简单的形式如下:

from PIL import Image
image = Image.open("1.jpeg")

在PIL库中,最常用的模块有Image,ImageDraw,ImageEnhance,ImageFile等。

PIL的下载地址为:http://www.pythonware.com/products/pil/

接下来学习PIL中一些常见的操作:

1. 改变图片大小

使用resize方法可以方便的对图片进行缩放

from PIL import Image
image = Image.open("1.jpeg")
new_img = image.resize((256,256),Image.BILINEAR)
new_img.save("2.jpeg")

2. 旋转图片

rotate方法可以进行旋转操作

from PIL import Image
image = Image.open("1.jpeg")
new_img = image.rotate(45)
new_img.save("2.jpeg")

3. 直线绘制

PIL库中的ImageDraw模块提供了图形绘制的基本功能,可以绘制直线,弧线,椭圆,矩形等等。

from PIL import Image,ImageDraw
image = Image.open("2.jpeg")
draw = ImageDraw.Draw(image)
width,height = image.size
draw.line(((0,0),(width-1,height-1)),fill=255)
draw.line(((0,height-1),(width-1,0)),fill=255)
image.save("2.jpeg")

4. 绘制圆

from PIL import Image,ImageDraw
image = Image.open("2.jpeg")
draw = ImageDraw.Draw(image)
width,height = image.size
draw.arc((0,0,width-1,height-1),0,360,fill=255)
image.save("2.jpeg")

5. 中文验证码实例

现在我们来研究利用Python如何生成中文验证码。其实这个问题简单,我们都知道验证码一般是用来防止网络机器

人采用次数的登录尝试破解密码,那么我们需要对这张图片随机生成中文字符来验证,一般来说会把图片的字表

现得很模糊,这样有效地防止机器人识别。所以我们除了要随机生成汉字,还要加入一些干扰线条等等。

# -*- coding: utf-8 -*-
from PIL import Image,ImageDraw,ImageFont
import random
import math, string 
class RandomChar():
 @staticmethod
 def Unicode():
  val = random.randint(0x4E00, 0x9FBF)
  return unichr(val) 
 @staticmethod
 def GB2312():
  head = random.randint(0xB0, 0xCF)
  body = random.randint(0xA, 0xF)
  tail = random.randint(0, 0xF)
  val = ( head << 8 ) | (body << 4) | tail
  str = "%x" % val
  return str.decode('hex').decode('gb2312') 
class ImageChar:
 def __init__(self, fontColor = (0, 0, 0),
           size = (100, 40),
           fontPath = 'SIMSUN.TTC',
           bgColor = (255, 255, 255),
           fontSize = 20):
  self.size = size
  self.fontPath = fontPath
  self.bgColor = bgColor
  self.fontSize = fontSize
  self.fontColor = fontColor
  self.font = ImageFont.truetype(self.fontPath, self.fontSize)
  self.image = Image.new('RGB', size, bgColor) 
 def rotate(self):
  self.image.rotate(random.randint(0, 30), expand=0) 
 def drawText(self, pos, txt, fill):
  draw = ImageDraw.Draw(self.image)
  draw.text(pos, txt, font=self.font, fill=fill)
 def randRGB(self):
  return (random.randint(0, 255),
      random.randint(0, 255),
      random.randint(0, 255)) 
 def randPoint(self):
  (width, height) = self.size
  return (random.randint(0, width), random.randint(0, height)) 
 def randLine(self, num):
  draw = ImageDraw.Draw(self.image)
  for i in range(0, num):
   draw.line([self.randPoint(), self.randPoint()], self.randRGB())
 def randChinese(self, num):
  gap = 5
  start = 0
  for i in range(0, num):
   char = RandomChar().GB2312()
   x = start + self.fontSize * i + random.randint(0, gap) + gap * i
   self.drawText((x, random.randint(-5, 5)), RandomChar().GB2312(), self.randRGB())
   self.rotate()
  self.randLine(18) 
 def save(self, path):
  self.image.save(path)
ic = ImageChar(fontColor=(100,211, 90))
ic.randChinese(4)
ic.save("1.jpeg")
关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

交换朋友的老婆2中字谜给看| 《公天天吃我奶躁我》| 蜜桃网站| 公共场合高潮(H)公交车| 绝美儿媳妇苏酥| 石头剪刀布谁输了看隐私| 看B站A8直播| 玉女献身台湾1988版主演是谁| 巨乳动漫| 十八岁免费观看全集高清| 国产 欧美一区二区三区| 百度CREATE2025| CSGO暴躁妹妹免费观看电视剧| 医生含着我的奶边摸边做视频| 《尼姑庵春梦》| 巨乳童颜| 办公室秘书胸置太薄胸凸出来| 秘书(高干)笔趣阁小说推荐| 玉米地被老头添的好爽| 《日本公与媳5》公妇伦| 窝窝影院在线观看免费播放电视剧 | 《熟妇的荡欲》免费看| 青梅竹马是消防员第一部 | 我是宿舍的寄吧套BY小说推荐 | 免费观看已满十八岁电视剧高清版 | 老师和学生深入交流人生奥秘| 我有一个朋友| 年经母年经3的| 男生把小困困放到女生困里视频| 《不戴套瑜伽教练3》| 免费看黄A片真人真事| 妈妈与儿子高清免费版电视剧 | 《大度》完整版| 圆滚滚的大扔子视频在线观看 | 无码亚洲一区二区三区| 学生妹高清版免费观看电视剧 | 一夹一摇怎么练出来| 二人世界高清视频播放| 刘老汉的儿媳小雪的背景资料| 《家访的老师》动漫在线观看| 麻花传剧原创MV免费播放小米车