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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述
完整代码
本篇链接:
https://blog.csdn.net/weixin_42385606/article/details/105995928

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章