图片的裁剪与切割

图片的裁剪与切割,第1张

1.图片的切割:将一张图像切割为多张图片,实现过程如下:

def cut(image):
    width, height = image.size
    width = int(width / 3 )  #对图片的宽进行等份,即需要将图片在宽度方向的均分未若等份
    height = int(height)
    box_list = []
    
    for i in range(0,1):#两重循环,生成3张图片基于原图的位置,外层是对高的等份,内层循环对宽度的循环等份
        for j in range(0,3):
            box = (j*width,i*height,(j+1)*width,(i+1)*height)
            box_list.append(box)

本例实现了对一张图像在宽度方向上实现了三等份,如果需要其他需求,在本例的基础上进行修改即可;

from os import listdir
from os.path import join

from PIL import Image



if __name__ == '__main__':
    #
    index=1
    test_image_path = ("/") # 图片文件夹路径
    image_filenames = [join(test_image_path, x) for x in listdir(test_image_path)]
    for image_filename in image_filenames:
            image = Image.open(image_filename)
            img= cut(image)
            for image in img:
                image.save('保存图像路径' + str(index) + '.jpg')
                index += 1

本段代码实现图片的批量处理;

2.图像的裁剪:在做传统图像处理或者深度学习的时候,采集到的图像可能存在诸多的无效信息,这些信息往往会增加计算量,而且会对图像处理过程造成影响,因此需要进行裁剪工作去除;具体实现如下:

def caijian(img):
    #rows,cols=img.shape[:2]
    width, height = image.size
    start_row,start_col=int(width*0.3),int(height*02.)
    end_row,end_col=int(width*0.75),int(height*0.8)
    cropped=img[start_row:end_row,start_col:end_col]
    return cropped

可根据本例修改为自己需要的。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/739516.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存