python PIL图像处理-生成图片验证码

生成效果如图:
代码
from PIL import Image,ImageDraw,ImageFont,ImageFilter
import random # 打开一个jpg图像文件:
im = Image.open('./image/mao.jpg')
# 获得图像尺寸:
w, h = im.size # 缩放到50%:
#im.thumbnail((w//2, h//2))
# 把缩放后的图像用jpeg格式保存:
im.save('./image/mao2.jpg', 'jpeg');
#im.save('./image/mao2.jpg') #生成图片验证码
# 随机字母:
def rndChar():
return chr(random.randint(65, 90)) # 随机颜色1:
def rndColor():
return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)) # 随机颜色2:
def rndColor2():
return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)) # 240 x 60:
width = 60 * 4
height = 60
image = Image.new('RGB', (width, height), (255, 255, 255)) # 创建Font对象:
#font = ImageFont.truetype('Arial.ttf', 36)
font =ImageFont.truetype('C:/windows/fonts/Arial.ttf', 36) # 创建Draw对象:
draw = ImageDraw.Draw(image) # 填充每个像素:
for x in range(width):
for y in range(height):
draw.point((x, y), fill=rndColor()) # 输出文字:
for t in range(4):
draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2()) # 模糊:
image = image.filter(ImageFilter.BLUR)
image.save('./image/code.jpg', 'jpeg')
image.show()
python PIL图像处理-生成图片验证码的更多相关文章
- python PIL 图像处理操作
python PIL 图像处理 # 导入Image库 import Image # 读取图片 im = Image.open("1234.jpg") # 显示图片 im.show( ...
- python PIL 图像处理
python PIL 图像处理 This blog is from: https://www.jianshu.com/p/e8d058767dfa Image读出来的是PIL的类型,而skimage. ...
- python PIL 图像处理库简介(一)
1. Introduction PIL(Python Image Library)是python的第三方图像处理库,但是由于其强大的功能与众多的使用人数,几乎已经被认为是python官方图像处 ...
- python PIL图像处理库
1. Introduction PIL(Python Image Library)是python的第三方图像处理库,但是由于其强大的功能与众多的使用人数,几乎已经被认为是python官方图像处理库了. ...
- python——PIL(图像处理库)
PIL(Python Imaging Library,python图像处理库)提供了通用的图像处理功能,以及大量有用的基本图像操作,如图像缩放,裁剪,旋转,颜色转换等. 1.打开图像并显示 from ...
- python PIL图像处理-框选
框选图中位置 代码 from PIL import Image,ImageDraw,ImageFont,ImageFilter import random #--------------------- ...
- python PIL图像处理-图片上添加文字
首先需要安装库pillow cmd安装命令:pip install pillow 安装完后,编写脚本如下: from PIL import Image, ImageDraw, ImageFont de ...
- python PIL图像处理
新建图像 # 三个参数分别代表图像的模式:常用的为RGB(3通道) .RGBA(4通道为透明通道,0为完全透明, 256为不透明) # 第二个参数为图像的长宽参数 # 第三个为默认的填充颜色,RGB时 ...
- Python,PIL压缩裁剪图片
自己写了用来压缩 DC 照片的,批量处理整目录文件,非常方便.需要安装 PIL #!/usr/bin/env python import Image import os import os.path ...
随机推荐
- RSA 数据加密和数字签名算法
PKCS8EncodedKeySpec pkcs8KeySpec = KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Pr ...
- v$open_cursor中的相同record
之前在查看v$open_cursor的时候,发现很多相同的record. 让我很疑惑, sid saddr sql_id 都相同,我就想 这不是一个cursor吗? 那为什么在open_cursor中 ...
- c# POST和GET方式通过server地址提交数据
1:POST方式提交: <strong><span style="font-size:14px;">private static string HttpPo ...
- oracle Plsql 运行update或者delete时卡死问题解决的方法
oracle Plsql 运行update或者delete时 遇到过Plsql卡死问题或者导致代码运行sql的时候就卡死. 在开发中遇到此问题的时候,本来把sql复制出来,在plsql中运行,Sql本 ...
- 《CS:APP》 chapter 9 Vitrual Memory 笔记
Vitrual Memory In order to manage memory more efficiently and with fewer errors, modern systems prov ...
- B5248 [2018多省省队联测]一双木棋 状压dp
这个题当时划水,得了二十分,现在来整一整. 这个题用状压来压缩边界线,然后通过记忆化搜索进行dp.我们可以观察到,其实每次转移,就是把一个1向左移一位.最后的状态设为0. 这其中还要有一个变量来记录谁 ...
- E20170815-mk
frame n. 框架; 边框; 眼镜框; 组织;
- HDU3085 Nightmare Ⅱ
题目: Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were ...
- CSS3 中弹性盒模型--容器的属性
1.display : flex | inline-flex注意,设为 Flex 布局以后,子元素的float.clear和vertical-align属性 将失效. 2.flex-direction ...
- Springboot统一跨域配置
前言:跨域是什么? 要知道跨域的概念,我们先明确怎样算是同一个域: 同一个域指的是同一协议,同一ip,同一端口 如果这三同中有一者不同就产生了跨域. 在做前后端分离的项目中,通过ajax请求后台端口时 ...