PIL簡介
什麼是PIL
PIL:是Python Image Library的縮寫,圖像處理的模塊。主要的類包括Image,ImageFont,ImageDraw,ImageFilterhtml
PIL的導入
首先須要安裝一下pillow包python
1
|
pip install pillow
|
而後就能夠調用PIL裏的類了django
1
2
3
4
|
from
PIL
import
Image
from
PIL
import
ImageFont
from
PIL
import
ImageDraw
from
PIL
import
ImageFilter
|
PIL經常使用方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
open
()
#打開圖片
new(mode,size,color)
#建立一張空白圖片
save(
"test.gif"
,
"GIF"
)
#保存(新圖片路徑和名稱,保存格式)
size()
#獲取圖片大小
thumbnail(weight,high)
#縮放圖片大小(寬,高)
show()
#顯示圖片
blend(img1,img2,alpha)
#兩張圖片相加,alpha表示img1和img2的比例參數。
crop()
#剪切,提取某個矩陣大小的圖像。它接收一個四元素的元組做爲參數,各元素爲(left, upper, right, lower),座標系統的原點(0, 0)是左上角。
rotate(
45
)
#逆時針旋轉45度
transpose()
#旋轉圖像
transpose(Image.FLIP_LEFT_RIGHT)
#左右對換。
transpose(Image.FLIP_TOP_BOTTOM)
#上下對換。
transpose(Image.ROTATE_90)
#旋轉 90 度角。
transpose(Image.ROTATE_180)
#旋轉 180 度角。
transpose(Image.ROTATE_270)
#旋轉 270 度角。
paste(im,box)
#粘貼box大小的im到原先的圖片對象中。
convert()
#用來將圖像轉換爲不一樣色彩模式。
filters()
#濾鏡
BLUR
#虛化
EMBOSS
resize((
128
,
128
))
#resize成128*128像素大小
convert(
"RGBA"
)
#圖形類型轉換
getpixel((
4
,
4
))
#獲取某個像素位置的值
putpixel((
4
,
4
),(
255
,
0
,
0
))
#寫入某個像素位置的值
|
PIL應用
咱們主要用PIL來生成一張驗證碼的隨機圖,下面,咱們就一步步來作一個小示例session
一、生成一張固定尺寸固定顏色的圖片
1
2
3
4
5
6
|
from
PIL
import
Image
# 獲取一個Image對象,參數分別是RGB模式。寬150,高30,紅色
image
=
Image.new(
'RGB'
,(
150
,
30
),
'red'
)
# 保存到硬盤,名爲test.png格式爲png的圖片
image.save(
open
(
'test.png'
,
'wb'
),
'png'
)
|
二、生成一張隨機顏色的圖片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
from
PIL
import
Image
import
random
def
getRandomColor():
'''獲取一個隨機顏色(r,g,b)格式的'''
c1
=
random.randint(
0
,
255
)
c2
=
random.randint(
0
,
255
)
c3
=
random.randint(
0
,
255
)
return
(c1,c2,c3)
# 獲取一個Image對象,參數分別是RGB模式。寬150,高30,隨機顏色
image
=
Image.new(
'RGB'
,(
150
,
30
),getRandomColor())
# 保存到硬盤,名爲test.png格式爲png的圖片
image.save(
open
(
'test.png'
,
'wb'
),
'png'
)
|
三、生成一張帶有固定字符串的隨機顏色的圖片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
from
PIL
import
Image
from
PIL
import
ImageDraw
from
PIL
import
ImageFont
import
random
def
getRandomColor():
'''獲取一個隨機顏色(r,g,b)格式的'''
c1
=
random.randint(
0
,
255
)
c2
=
random.randint(
0
,
255
)
c3
=
random.randint(
0
,
255
)
return
(c1,c2,c3)
# 獲取一個Image對象,參數分別是RGB模式。寬150,高30,隨機顏色
image
=
Image.new(
'RGB'
,(
150
,
30
),getRandomColor())
# 獲取一個畫筆對象,將圖片對象傳過去
draw
=
ImageDraw.Draw(image)
# 獲取一個font字體對象參數是ttf的字體文件的目錄,以及字體的大小
font
=
ImageFont.truetype(
"kumo.ttf"
,size
=
32
)
# 在圖片上寫東西,參數是:定位,字符串,顏色,字體
draw.text((
20
,
0
),
'fuyong'
,getRandomColor(),font
=
font)
# 保存到硬盤,名爲test.png格式爲png的圖片
image.save(
open
(
'test.png'
,
'wb'
),
'png'
)
|
效果:app
四、生成一張帶有隨機字符串隨機顏色的圖片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
from
PIL
import
Image
from
PIL
import
ImageDraw
from
PIL
import
ImageFont
import
random
def
getRandomColor():
'''獲取一個隨機顏色(r,g,b)格式的'''
c1
=
random.randint(
0
,
255
)
c2
=
random.randint(
0
,
255
)
c3
=
random.randint(
0
,
255
)
return
(c1,c2,c3)
def
getRandomStr():
'''獲取一個隨機字符串,每一個字符的顏色也是隨機的'''
random_num
=
str
(random.randint(
0
,
9
))
random_low_alpha
=
chr
(random.randint(
97
,
122
))
random_upper_alpha
=
chr
(random.randint(
65
,
90
))
random_char
=
random.choice([random_num, random_low_alpha, random_upper_alpha])
return
random_char
# 獲取一個Image對象,參數分別是RGB模式。寬150,高30,隨機顏色
image
=
Image.new(
'RGB'
,(
150
,
30
),getRandomColor())
# 獲取一個畫筆對象,將圖片對象傳過去
draw
=
ImageDraw.Draw(image)
# 獲取一個font字體對象參數是ttf的字體文件的目錄,以及字體的大小
font
=
ImageFont.truetype(
"kumo.ttf"
,size
=
26
)
for
i
in
range
(
5
):
# 循環5次,獲取5個隨機字符串
random_char
=
getRandomStr()
# 在圖片上一次寫入獲得的隨機字符串,參數是:定位,字符串,顏色,字體
draw.text((
10
+
i
*
30
,
0
),random_char , getRandomColor(), font
=
font)
# 保存到硬盤,名爲test.png格式爲png的圖片
image.save(
open
(
'test.png'
,
'wb'
),
'png'
)
|
效果:dom
五、生成一張帶有噪點的驗證碼圖片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
from
PIL
import
Image
from
PIL
import
ImageDraw
from
PIL
import
ImageFont
import
random
def
getRandomColor():
'''獲取一個隨機顏色(r,g,b)格式的'''
c1
=
random.randint(
0
,
255
)
c2
=
random.randint(
0
,
255
)
c3
=
random.randint(
0
,
255
)
return
(c1,c2,c3)
def
getRandomStr():
'''獲取一個隨機字符串,每一個字符的顏色也是隨機的'''
random_num
=
str
(random.randint(
0
,
9
))
random_low_alpha
=
chr
(random.randint(
97
,
122
))
random_upper_alpha
=
chr
(random.randint(
65
,
90
))
random_char
=
random.choice([random_num, random_low_alpha, random_upper_alpha])
return
random_char
# 獲取一個Image對象,參數分別是RGB模式。寬150,高30,隨機顏色
image
=
Image.new(
'RGB'
,(
150
,
30
),getRandomColor())
# 獲取一個畫筆對象,將圖片對象傳過去
draw
=
ImageDraw.Draw(image)
# 獲取一個font字體對象參數是ttf的字體文件的目錄,以及字體的大小
font
=
ImageFont.truetype(
"kumo.ttf"
,size
=
26
)
for
i
in
range
(
5
):
# 循環5次,獲取5個隨機字符串
random_char
=
getRandomStr()
# 在圖片上一次寫入獲得的隨機字符串,參數是:定位,字符串,顏色,字體
draw.text((
10
+
i
*
30
,
0
),random_char , getRandomColor(), font
=
font)
# 噪點噪線
width
=
150
height
=
30
# 劃線
for
i
in
range
(
5
):
x1
=
random.randint(
0
,width)
x2
=
random.randint(
0
,width)
y1
=
random.randint(
0
,height)
y2
=
random.randint(
0
,height)
draw.line((x1,y1,x2,y2),fill
=
getRandomColor())
# 畫點
for
i
in
range
(
30
):
draw.point([random.randint(
0
, width), random.randint(
0
, height)], fill
=
getRandomColor())
x
=
random.randint(
0
, width)
y
=
random.randint(
0
, height)
draw.arc((x, y, x
+
4
, y
+
4
),
0
,
90
, fill
=
getRandomColor())<br>
# 保存到硬盤,名爲test.png格式爲png的圖片
image.save(
open
(
'test.png'
,
'wb'
),
'png'
)
|
效果:post
六、對驗證碼圖片生成進行封裝
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
from
PIL
import
Image
from
PIL
import
ImageDraw
from
PIL
import
ImageFont
import
random
class
ValidCodeImg:
def
__init__(
self
,width
=
150
,height
=
30
,code_count
=
5
,font_size
=
32
,point_count
=
20
,line_count
=
3
,img_format
=
'png'
):
'''
能夠生成一個通過降噪後的隨機驗證碼的圖片
:param width: 圖片寬度 單位px
:param height: 圖片高度 單位px
:param code_count: 驗證碼個數
:param font_size: 字體大小
:param point_count: 噪點個數
:param line_count: 劃線個數
:param img_format: 圖片格式
:return 生成的圖片的bytes類型的data
'''
self
.width
=
width
self
.height
=
height
self
.code_count
=
code_count
self
.font_size
=
font_size
self
.point_count
=
point_count
self
.line_count
=
line_count
self
.img_format
=
img_format
@staticmethod
def
getRandomColor():
'''獲取一個隨機顏色(r,g,b)格式的'''
c1
=
random.randint(
0
,
255
)
c2
=
random.randint(
0
,
255
)
c3
=
random.randint(
0
,
255
)
return
(c1,c2,c3)
@staticmethod
def
getRandomStr():
'''獲取一個隨機字符串,每一個字符的顏色也是隨機的'''
random_num
=
str
(random.randint(
0
,
9
))
random_low_alpha
=
chr
(random.randint(
97
,
122
))
random_upper_alpha
=
chr
(random.randint(
65
,
90
))
random_char
=
random.choice([random_num, random_low_alpha, random_upper_alpha])
return
random_char
def
getValidCodeImg(
self
):
# 獲取一個Image對象,參數分別是RGB模式。寬150,高30,隨機顏色
image
=
Image.new(
'RGB'
,(
self
.width,
self
.height),
self
.getRandomColor())
# 獲取一個畫筆對象,將圖片對象傳過去
draw
=
ImageDraw.Draw(image)
# 獲取一個font字體對象參數是ttf的字體文件的目錄,以及字體的大小
font
=
ImageFont.truetype(
"kumo.ttf"
,size
=
self
.font_size)
temp
=
[]
for
i
in
range
(
self
.code_count):
# 循環5次,獲取5個隨機字符串
random_char
=
self
.getRandomStr()
# 在圖片上一次寫入獲得的隨機字符串,參數是:定位,字符串,顏色,字體
draw.text((
10
+
i
*
30
,
-
2
),random_char ,
self
.getRandomColor(), font
=
font)
# 保存隨機字符,以供驗證用戶輸入的驗證碼是否正確時使用
temp.append(random_char)
valid_str
=
"".join(temp)
# 噪點噪線
# 劃線
for
i
in
range
(
self
.line_count):
x1
=
random.randint(
0
,
self
.width)
x2
=
random.randint(
0
,
self
.width)
y1
=
random.randint(
0
,
self
.height)
y2
=
random.randint(
0
,
self
.height)
draw.line((x1,y1,x2,y2),fill
=
self
.getRandomColor())
# 畫點
for
i
in
range
(
self
.point_count):
draw.point([random.randint(
0
,
self
.width), random.randint(
0
,
self
.height)], fill
=
self
.getRandomColor())
x
=
random.randint(
0
,
self
.width)
y
=
random.randint(
0
,
self
.height)
draw.arc((x, y, x
+
4
, y
+
4
),
0
,
90
, fill
=
self
.getRandomColor())
# 在內存生成圖片
from
io
import
BytesIO
f
=
BytesIO()
image.save(f,
self
.img_format)
data
=
f.getvalue()
f.close()
return
data,valid_str
if
__name__
=
=
'__main__'
:
img
=
ValidCodeImg()
data, valid_str
=
img.getValidCodeImg()
print
(valid_str)
f
=
open
(
'test.png'
,
'wb'
)
f.write(data)
|
效果:字體
七、應用到實際開發中
login.htmlurl
1
2
|
<
input
id
=
"valid-inp"
name
=
"validcode"
class
=
"form-control"
type
=
"password"
placeholder
=
"請輸入驗證碼"
autocomplete
=
"off"
>
<span
id
=
"valid-img"
><img
id
=
"img"
src
=
"/get_valid_img"
title
=
"點擊再換一張"
alt
=
"驗證碼圖片"
><
/
span>
|
urls.pyspa
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from
django.conf.urls
import
url
from
django.contrib
import
admin
from
blog
import
views
urlpatterns
=
[
url(r
'^admin/'
, admin.site.urls),
url(r
'^$'
,views.Main.as_view(),name
=
'main'
),
url(r
'^login$'
,views.Login.as_view(),name
=
'login'
),
# 登陸頁面驗證碼圖片請求
url(r
'^get_valid_img'
,views.GetValidImg.as_view(),name
=
'get_valid_img'
),
]
|
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
class
Main(View):
def
get(
self
,request):
return
render(request,
'main.html'
)
class
Login(View):
def
get(
self
,request):
return
render(request,
'login.html'
)
def
post(
self
,request):
username
=
request.POST.get(
'username'
)
password
=
request.POST.get(
'password'
)
valid_code
=
request.POST.get(
'valid_code'
)
# print(valid_code)
# print(request.session.get('valid_code'))
if
valid_code.upper() !
=
request.session.get(
'valid_code'
).upper():
return
JsonResponse({
'state'
:
False
,
'msg'
:
'驗證碼錯誤'
})
user
=
auth.authenticate(request,username
=
username,password
=
password)
if
user:
# 登陸成功,經過auth的login方法將用戶寫到session中
auth.login(request,user)
# 提交表單登陸成功後跳轉到用戶本身的博客首頁
redirect_url
=
'/{}'
.
format
(user.username)
return
JsonResponse({
'state'
:
True
,
'msg'
:
'登陸成功!'
,
'url'
:redirect_url})
else
:
return
JsonResponse({
'state'
:
False
,
'msg'
:
'用戶名或密碼錯誤!'
})
class
GetValidImg(View):
def
get(
self
,request):
obj
=
ValidCodeImg()
img_data,valid_code
=
obj.getValidCodeImg()
request.session[
'valid_code'
]
=
valid_code
return
HttpResponse(img_data)
|