python批量图片切割(opencv版)

[1].[代码] [Python]代码 跳至 [1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import cv2.cv as cv
import os
 
imgTypes = [ ".png" , ".jpg" , ".bmp" ]
 
for root,dirs,files in os.walk( "." ):
     for afile in files:
         ffile = root + "\\" + afile
         if ffile[ffile.rindex( "." ):].lower() in imgTypes:
             img = cv.LoadImage(ffile)
             if img.width>img.height:
                 cv.SetImageROI(img,( 0 , 0 ,img.width / 2 ,img.height))
                 cv.SaveImage(ffile[:ffile.rindex( "." )] + "_1" + ffile[ffile.rindex( "." ):],img)
                 cv.SetImageROI(img,(img.width / 2 , 0 ,img.width / 2 ,img.height))
                 cv.SaveImage(ffile[:ffile.rindex( "." )] + "_0" + ffile[ffile.rindex( "." ):],img)
                 os.remove(ffile)

你可能感兴趣的:(python)