Pillow-Image模块 图像基本几何变换和颜色空间变换

读取图片

from PIL import Image

image_path='./images/capf.jpg'

image=Image.open(image_path)

调整大小 resize

image_width,image_height=image.size
resize_path='./images/capf_resize.jpg'

resize_image=image.resize((image_width*2,image_height*2))

try:
    resize_image.save(resize_path)
    print('图像保存成功!')
except IOError:
    print('图像保存失败!')
图像保存成功!
旋转 rotate
rotate_path='./images/capf_rotate.jpg'

rotate_image=image.rotate(60)

try:
    rotate_image.save(rotate_path)
    print('图像保存成功!')
except IOError:
    print('图像保存失败!')
图像保存成功!
转置 transpose
flip_lr_path='./images/capf_flip_lr.jpg'
flip_tb_path='./images/capf_flip_tb.jpg'
trans90_path='./images/capf_trans90.jpg'

flip_lr_image=image.transpose(Image.FLIP_LEFT_RIGHT)
flip_tb_image=image.transpose(Image.FLIP_TOP_BOTTOM)
trans90_image=image.transpose(Image.ROTATE_90)

try:
    flip_lr_image.save(flip_lr_path)
    flip_tb_image.save(flip_tb_path)
    trans90_image.save(trans90_path)
    print('图像保存成功!')
except IOError:
    print('图像保存失败!')
图像保存成功!
颜色空间转换 convert
lab_path='./images/capf_lab.jpg'

lab_image=image.convert('L')

try:
    lab_image.save(lab_path)
    print('图像保存成功!')
except IOError:
    print('图像保存失败!')
图像保存成功!

你可能感兴趣的:(后端开发)