赞
踩
做图像处理需要大批量的修改图片尺寸来做训练样本,为此本程序借助opencv来实现大批量的剪切图片。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import cv2
import
cv2
import os
os
def cutimage(dir,suffix):
def
cutimage(
dir
,suffix):
for root,dirs,files in os.walk(dir):
for
root,dirs,files
in
os.walk(
):
for file in files:
file
files:
filepath = os.path.join(root, file)
filepath
=
os.path.join(root,
)
filesuffix = os.path.splitext(filepath)[1][1:]
filesuffix
os.path.splitext(filepath)[
][
:]
if filesuffix in suffix: #遍历找到指定后缀的文件名["jpg",png]等
if
suffix:
#遍历找到指定后缀的文件名["jpg",png]等
image = cv2.imread(file) #opencv剪切图片
image
cv2.imread(
#opencv剪切图片
#cv2.imshow(file,image)
dim =(242,200) #指定尺寸w*h
dim
(
242
,
200
#指定尺寸w*h
resized =cv2.resize(image,dim,interpolation = cv2.INTER_AREA) #这里采用的插值法是INTER_LINEAR
resized
cv2.resize(image,dim,interpolation
cv2.INTER_AREA)
#这里采用的插值法是INTER_LINEAR
#cv2.imshow("resize:%s"%file,resized)
cv2.imwrite("../cv/%s"%file,resized) #保存文件
cv2.imwrite(
"../cv/%s"
%
,resized)
#保存文件
cv2.waitKey(0) #退出
cv2.waitKey(
0
#退出
suffix = ["jpg"]
suffix
[
"jpg"
]
dir = '.'
'.'
cutimage(dir,suffix)
,suffix)