python通过opencv实现批量剪切图片

时间:2022-11-25 14:36:33

上一篇文章中,我们介绍了python实现图片处理和特征提取详解,这里我们再来看看Python通过OpenCV实现批量剪切图片,具体如下。

做图像处理需要大批量的修改图片尺寸来做训练样本,为此本程序借助opencv来实现大批量的剪切图片。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import cv2
import os
def cutimage(dir,suffix):
 for root,dirs,files in os.walk(dir):
  for file in files:
   filepath = os.path.join(root, file)
   filesuffix = os.path.splitext(filepath)[1][1:]
   if filesuffix in suffix:  #遍历找到指定后缀的文件名["jpg",png]等
    image = cv2.imread(file) #opencv剪切图片 
    #cv2.imshow(file,image)
    dim =(242,200)      #指定尺寸w*h
    resized =cv2.resize(image,dim,interpolation = cv2.INTER_AREA) #这里采用的插值法是INTER_LINEAR
    #cv2.imshow("resize:%s"%file,resized)
    cv2.imwrite("../cv/%s"%file,resized) #保存文件
 cv2.waitKey(0)     #退出
 
suffix = ["jpg"]
dir = '.'
cutimage(dir,suffix)

有一些值需要自己更改,比如保存路径和保存名称。

总结

以上就是本文关于python通过opencv实现批量剪切图片的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/eds95/article/details/68489522