Python--Image库图片遮挡、剪切、位移

python的Image库非常强大,几乎被认为是python官方图像处理库。这里简单的介绍几种我自己最近用到的处理,包括位移、剪切和遮挡。以后再用到其他功能,就继续更新吧。

1、位移

Imagechops模块除了offset这个偏移方法,还有很多函数,见这里。

def shift(im):
    plt.figure()
    plt.subplot(1,2,1)
    img = Image.open(im)
    plt.imshow(img)
    img2 = ImageChops.offset(img,200,100)  # 水平位移:200,垂直位移:100
    plt.subplot(1,2,2)
    plt.imshow(img2)
    plt.show()
    #img = img.resize((224, 224), resample=Image.LANCZOS)
    return img2

Python--Image库图片遮挡、剪切、位移_第1张图片

2、遮挡

# 遮挡
def paste(im):
    plt.figure()
    plt.subplot(1, 2, 1)
    img = Image.open(im)
    plt.imshow(img)
    img2 = img.crop((0,0,80,80))  #裁剪原图中一部分作为覆盖图片
    img.paste(img2, (150, 150, 150+img2.size[0], 150+img2.size[1]))  #第二个参数是覆盖位置
    plt.subplot(1, 2, 2)
    plt.imshow(img)
    plt.show()
    return img

Python--Image库图片遮挡、剪切、位移_第2张图片

3、剪切

#剪切
def shear(im):
    plt.figure()
    plt.subplot(1, 2, 1)
    img = Image.open(im)
    plt.imshow(img)
    #image = cv2.resize(image,(224,224))
    cropped = img.crop((100,100,500,500))  #坐标从左上开始
    plt.subplot(1, 2, 2)
    plt.imshow(cropped)
    plt.show()
    return cropped

Python--Image库图片遮挡、剪切、位移_第3张图片
完整代码my github
本篇链接:
https://blog.csdn.net/weixin_42385606/article/details/105995928

你可能感兴趣的:(python)