PIL 是python比较常用的图像处理库接下来简单讲解下Pillow 库的使用
00 :PIL 库的安装
pip install Pillow
PIL库的导入
import PIL
或者
from PIL import Image,ImageFilter
基础使用:
01 加载图片 Imgge.open("路径")
from PIL import Image,ImageFilter
# 加载图片
imagel = Image.open("files/text.jpg")02显示图片:
imagel.show() #imagel为上面打开的图片
03 保存图片 Imgge.save("路径")
imagel.save("files/2.jpg")04剪切
imagel2 = imagel.crop((1,1,100,100))#参数 左上角x,y 右下角x,y 向右向下
05粘贴
贴图 把imfgel1贴到imagle上
# 加载图片
imagel = Image.open("files/1.jpg")#原图
imagel1 = Image.open("files/剪切.jpg")#贴图
# 贴图
imagel.paste(imagel1,(10,10)) #参数 图 坐标
imagel.show()多图粘贴
#建空白图 nwe(模式 大小 颜色(RGB(255,255,255)
#模式 RGB(3颜色)/RGBA(有透明度
empty = Image.new("RGB",(2200,2200),(255,255,0))
# empty.show()
empty.paste(imagel,(0,0))
empty.paste(imagel1,(1100,0))
empty.show()06缩放
# 图片对象.thumbnail(大小)-会按比例 image1.thumbnail((100,100))
07镜像
# 加载图片
image1 = Image.open("files/1.jpg")
image2 = Image.open("files/剪切.jpg")
#左右镜像
img_lr = image1.transpose(Image.FLIP_LEFT_RIGHT)
img_lr.show()
#上下
img_lr = image1.transpose(Image.FLIP_TOP_BOTTOM)
img_lr.show()08文字水印 #字体可以到网上找也可一复制电脑自带的路径C:\Windows\Fonts
from PIL import Image,ImageFilter,ImageFont,ImageDraw
# 加载图片
image1 = Image.open("files/1.jpg")
image2 = Image.open("files/剪切.jpg")
#创建文字对象
font = ImageFont.truetype("simkai.ttf",100)#我是复制C:\Windows\Fonts里面的可以到网上找其他字体
#
draw = ImageDraw.Draw(image1)
draw.text((100,100),"贴的字",font=font,fill=(255,0,0))#坐标 字 字体对象 颜色
image1.show()09自定义颜色快
from PIL import Image,ImageFilter,ImageFont,ImageDraw
# 加载图片
image1 = Image.open("files/1.jpg")
image2 = Image.new("RGB",(600,600),(255,255,255))
# image2.show()
draw = ImageDraw.Draw(image2)
# 坐标颜色
# draw.point((100,100),(0,0,0))
# draw.point((100,101),(0,0,0))
# draw.point((100,102),(0,0,0))
# draw.point((100,103),(0,0,0))
# draw.point((100,104),(0,0,0))
# draw.point((100,105),(0,0,0))
# draw.point((100,106),(0,0,0))
# draw.point((100,107),(0,0,0))
for x in range(1,255):
for y in range(1,255):
draw.point((x, y), (x, y, 0))
# draw.point((x, y), (0, x, y))
# draw.point((x, y), (x, 0, y))
# draw.point((x, y), (y, x, 0))
# draw.point((x, y), (y, 0, x))
# draw.point((x, y), (y, 0, x))
image2.show()10 镜像效果
from PIL import Image,ImageFilter
# 加载图片
imagel = Image.open("files/text.jpg")
#显示图片
# imagel.show()
# 3保存图片
# imagel.save("files/2.jpg")
# # 滤镜效果
# # 1条件系统滤镜效果
# # 图片对象.filter
# imagel2 = imagel.filter(ImageFilter.EMBOSS)#浮雕效果
# imagel2.show()
# imagel2.save("files/浮雕.jpg")
# imagel3 = imagel.filter(ImageFilter.CONTOUR)#铅笔画
# imagel3.show()
# imagel3.save("files/铅笔画.jpg")
# imagel4 = imagel.filter(ImageFilter.BLUR)#模糊效果
# imagel4.show()
# imagel4.save("files/铅笔画.jpg")
# imagel5 = imagel.filter(ImageFilter.EDGE_ENHANCE)#增强效果
# imagel5.show()
# imagel6 = imagel.filter(ImageFilter.SMOOTH)#
# imagel.show()
# imagel6.show()
# 自写
# class JC_ENHANCE(ImageFilter.BuiltinFilter):
# name = "jc-enhance"
# # fmt: off
# # (3,3)3行3列
# filterargs = (3, 3), 2, 0, (#
# -1, -1, -1,
# -1, 10, -1,
# -1, -1, -1,
# )#中间值是背景亮度
#
# imagel6 = imagel.filter(JC_ENHANCE) #
# # imagel.show()
# imagel6.show()做个简单的认证码:
import PIL
from PIL import Image,ImageFilter,ImageFont,ImageDraw
from random import randint
#创建文字对象
font = ImageFont.truetype("simkai.ttf",30)
# 加载图片
image1 = Image.open("files/1.jpg")
image2 = Image.new("RGB",(120,60),(255,255,255))
# image2.show()
draw = ImageDraw.Draw(image2)
def rand_color():
r = randint(0,255)
g = randint(0,255)
b = randint(0,255)
return r,g,b
#背景
for x in range(0,120):
for y in range(0,60):
draw.point((x, y), (rand_color()))
image2 = image2.filter(ImageFilter.BLUR)
draw = ImageDraw.Draw(image2)
nums = (randint(0,9),randint(0,9),randint(0,9),randint(0,9))
# for num in nums:
nums = []
for x in range(4):
num = randint(0,9)
nums.append(num)
# num = chr(randint(0x4e00,0x9fa5))
# num = chr(randint(65,97+25))
draw.text((5+30*x, randint(10,30)), str(num), font=font, fill=(rand_color()))
# draw.text((0,0),"我爱你",font=font,fill=(0,0,0))#坐标 字 字体对象 颜色
image2.show()
print("".join('%s' %id for id in nums))
a = input("输入")
print(a)
if a == "".join('%s' %id for id in nums):
image1.show()本次源码:关注公众号,回复 Pillow
谢谢收看!










