pillow包

pillow包

pillow:(了解)(python image library)是一个有关图像图片处理的包,这个包底层用的C C++,但PIL包是python2下使用。所以又更新了一个适合python3版本的、基于PIL包的新包pillow。

安装pillow:

(cmd) pip install pillow

引入:from PIL import Image, ImageFilter

image_path = Image.open('img.jpg')    
# image_path = Image.open('图片相对路径')

print(image_path.size)
width, height = image_path.size
print(width, height)

1.缩放 thumbnail

image_path.thumbnail((800,600))

两个参数,第一个参数是宽,第二个参数是高

2.保存为新图片 save

image_path.save('img', 'jpeg')    
  
# image_path.save('文件名', '保存文件格式(如:jpeg)')

3.旋转 rotate

image_rotate = image_path.rotate(90)
image_rotate.show()     
  
# 打开文件,用于临时打开

4.滤镜
ImageFilter(滤镜)
GaussianBlur高斯模糊
Color3DLUT颜色三地
BLUR默认颜色

image_ImageFilter = image_path1.filter(ImageFilter.BLUR)
image_ImageFilter.show()

image_path1.save('demo3.jpg','jpeg')
随机生成验证码

(了解)RGB:red green blue 色光三原色,(11,225,5)

引入:
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import random

1.随机字母

def random_char():
    return chr(random.randint(65, 90))

2.随机数字

def random_num():
    return random.randint(0,9)

3.随机字体颜色

def random_color():
    return (random.randint(150,255),random.randint(150,255),random.randint(150,255))

4.随机背景颜色

def random_color2():
    return (random.randint(30, 120), random.randint(30, 120), random.randint(30, 120))

5.生成空白背景图层

image = Image.new('RGB', size=(240, 60), color=(255, 255, 255))

6.生成绘制对象

draw = ImageDraw.Draw(image)

7.字体对象 ,字体、字号

font = ImageFont.truetype('arial.ttf', 36)

8.循环像素点并填充颜色

for x in range(0, 240):
    for y in range(0, 60):
        draw.point(xy=(x, y), fill=random_color2())

9.生成文字

for t in range(0, 4):
    draw.text((60*t+20, 10), random_char(), font=font, fill=random_color())

10.加模糊滤镜

image = image.filter(ImageFilter.BLUR)

11.保存

image.save('demo4.jpg', 'jpeg')

画点完整代码:

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


class Picture(object):
    def __init__(self, text_str, size, background):
        '''
        text_str: 验证码显示的字符组成的字符串
        size:  图片大小
        background: 背景颜色
        '''
        self.text_list = list(text_str)
        self.size = size
        self.background = background

    def create_pic(self):
        '''
        创建一张图片
        '''
        self.width, self.height = self.size
        self.img = Image.new("RGB", self.size, self.background)
        # 实例化画笔
        self.draw = ImageDraw.Draw(self.img)

    def create_point(self, num, color):
        '''
        num: 画点的数量
        color: 点的颜色
        功能:画点
        '''
        for i in range(num):
            self.draw.point(
                (random.randint(0, self.width), random.randint(0, self.height)),
                fill=color
            )

    def create_line(self, num, color):
        '''
        num: 线条的数量
        color: 线条的颜色
        功能:画线条
        '''
        for i in range(num):
            self.draw.line(
                [
                    (random.randint(0, self.width), random.randint(0, self.height)),
                    (random.randint(0, self.width), random.randint(0, self.height))
                ],
                fill=color
            )

    def create_text(self, font_type, font_size, font_color, font_num, start_xy):
        '''
        font_type: 字体
        font_size: 文字大小
        font_color: 文字颜色
        font_num:  文字数量
        start_xy:  第一个字左上角坐标,元组类型,如 (5,5)
        功能: 画文字
        '''
        font = ImageFont.truetype(font_type, font_size)
        self.draw.text(start_xy, " ".join(random.sample(self.text_list, font_num)), font=font, fill=font_color)

    def opera(self):
        '''
        功能:给画出来的线条,文字,扭曲一下,缩放一下,位移一下,滤镜一下。
        就是让它看起来有点歪,有点扭。
        '''
        params = [
            1 - float(random.randint(1, 2)) / 100,
            0,
            0,
            0,
            1 - float(random.randint(1, 10)) / 100,
            float(random.randint(1, 2)) / 500,
            0.001,
            float(random.randint(1, 2)) / 500
        ]
        self.img = self.img.transform(self.size, Image.PERSPECTIVE, params)
        self.img = self.img.filter(ImageFilter.EDGE_ENHANCE_MORE)


if __name__ == "__main__":
    strings = "abcdefghjkmnpqrstwxyz23456789ABCDEFGHJKLMNPQRSTWXYZ"
    size = (150, 50)
    background = 'white'
    pic = Picture(strings, size, background)
    pic.create_pic()
    pic.create_point(500, (220, 220, 220))
    pic.create_line(30, (220, 220, 220))
    pic.create_text("simsun.ttc", 24, (0, 0, 205), 5, (7, 7))
    pic.opera()
    pic.img.show()

你可能感兴趣的:(包)