1. 作用
图像处理库
2. 操作
from PIL import Image # 引入Image类
# 创建一个新的图片
def img_create(outfile, mode='RGB', size=(100, 100), color='red'):
new_img= Image.new(mode, size, color) # 创建一个新的Image实例
new_img.save(outfile)
# 展示图片
def img_open(infile):
with Image.open(infile) as im: # 读取图片,读取不成功抛出IOError
print(im.format, im.size, im.mode) # 图片格式, 图片像素, 图片模式(一般RGB)
im.show() # debian下不管用,需要安装xv
# 读写图片
def img_save(infile, outfile):
try:
Image.open(infile).save(outfile) # 打开图片并保存为另外的格式
# open时,pillow获取文件信息如format、mode,余下数据需要时出处理
except IOError:
print('connot convert {}'.format(infile))
# 转换模式 mode
def img_convert(infile, outfile, mode):
with Image.open(infile) as im:
im_copy = im.copy()
im_copy.convert(mode).save(outfile)
# mode参数: 1\L\P\RGB\RGBA\CMYK\YCbCr\LAB\HSV\I\F
# 缩略图
def img_thumbnail(infile, outfile, size=(100, 200)):
with Image.open(infile) as im:
im.thumbnail(size)
im.save(outfile)
# 复制图片
def img_copy(infile, outfile):
with Image.open(infile) as im:
im.copy().save(outfile)
# 剪裁图片
def img_crop(infile, outfile, size=(100, 100, 400, 400)):
with Image.open(infile) as im:
im.crop(size).save(outfile) # size=(左, 顶, 右, 底),左上角为坐标原点,
# (100, 100, 400, 400) 表示300*300的像素,就是右-左, 底-顶
# 粘贴图片
def img_paste(infile, outfile, size=(200, 200, 400, 400)):
with Image.open(infile) as im:
im_crop = im.crop(size)
crop_width, crop_height = im_crop.size
width, height = im.size
im_copy = im.copy()
for left in range(0, width, crop_width):
for top in range(0, height, crop_height):
im_copy.paste(im_crop, (left, top))
im_copy.save(outfile)
# 调整图片大小
def img_resize(infile, outfile, size=(200, 400), expand=True):
with Image.open(infile) as im:
im.resize(size, expand).save(outfile) # 参数expand=True适应原图片
# 翻转图片
def img_transpose(infile, outfile, flip_point): # flip_point为翻转参数
with Image.open(infile) as im:
im.transpose(flip_point).save(outfile)
# 参数flip_point :
# Image.ROTATE_90/180/270 类似rotate()没有性能差别。
# Image.FLIP_LEFT_RIGHT
# Image.FLIP_TOP_BUTTOM
# 旋转图片
def img_rotate(infile, outfile, size=90):
with Image.open(infile) as im:
im.rotate(size).save(outfile)
# 移动图片
def img_roll(infile, outfile, delta):
with Image.open(infile) as im:
image = im.copy() # 复制图片
x, y = image.size
delta = delta % x # 防止delta比x大
if delta == 0:
image.save(outfile)
return
part1 = image.crop((0, 0, delta, y))
part2 = image.crop((delta, 0, x, y))
image.paste(part2, (0, 0, x - delta, y))
image.paste(part1, (x - delta, 0, x, y))
image.save(outfile)
from PIL import ImageFilter
# 图片过滤
def img_filter(infile, outfile, filter_point): # filter_point模糊模式
with Image.open(infile) as im:
im.filter(filter_point).save(outfile)
# 参数filter_point参照pillow文档ImageFileter
def img_point(infile, outfile):
with Image.open(infile) as im:
im_point = im.point(lambda i: i * 2) # 高级函数,日,类似Map
im_point.save(outfile)
###要进行图案、文字的绘制,可使用ImageDraw###
if __name__ == '__main__':
# img_create('timg_new.jpeg', mode='RGB', size=(100, 100), color='blue')
# img_open('timg_new.jpeg')
# img_save('timg.jpeg', 'timg_conv.png')
# img_convert('timg.jpeg', 'timg_convert.jpeg', 'L')
# img_thumbnail('timg.jpeg', 'timg_thum.jpeg', size=(100, 200))
# img_copy('timg.jpeg', 'timg_copy.jpeg')
# img_crop('timg.jpeg', 'timg_crop.jpeg', size=(100, 100, 400, 400))
# img_paste('timg.jpeg', 'timg_past.jpeg', size=(200, 200, 400, 400))
# img_resize('timg.jpeg','timg_resi.jpeg', size=(200, 400))
# img_transpose('timg.jpeg', 'timg_righ.jpeg', Image.FLIP_LEFT_RIGHT)
# img_transpose('timg.jpeg', 'timg_bott.jpeg', Image.FLIP_TOP_BOTTOM)
# img_rotate('timg.jpeg', 'timg_rota.jpeg', size=90)
# img_roll('timg.jpeg', 'timg_roll.jpeg', 500)
# img_filter('timg.jpeg', 'timg_filt.jpeg', ImageFilter.EMBOSS)
img_point('timg.jpeg', 'timg_poin.jpeg')