使用python批量裁剪图片大小

原图是这样的,从数据库导出来发现右半边有问题,把图片右边的去掉。Ubuntu系统,python作为一种批处理工具是非常可行有用的。

微信截图_20201110095544.png

#coding:utf-8
import os
import cv2 
def ChangeFileName(path,Qianzui,Houzui):#, dist, pos_or_neg
    filelist = os.listdir(path)
#    print("filelist:",filelist)
    count=0
    for file in filelist:
        print(file)
    for file in filelist:   
        Olddir=os.path.join(path,file)  
        if os.path.isdir(Olddir):  
            continue
        filename=os.path.splitext(file)[0]   
        filetype=os.path.splitext(file)[1]
        Newdir=os.path.join(path,Qianzui+str(count).zfill(1)+Houzui+filetype)  
        os.rename(Olddir,Newdir)
        count+=1
def cutPicture():
    filelist = os.listdir(path)
    """简化版,读到文件名之后不保存,立即进行操作"""          
    count=7
    for file in filelist:
        print(file)
    for file in filelist:                                      # 文件遍历
        line = path+ '/'+ file
        print(line)
        image = cv2.imread(line)                          # 读取文件名对应的图片
        cropImg = image[0:288,0:360]                 # 裁剪 # 先y范围,后x范围,每个的第三个为采样步长
        cv2.imwrite("image1/" + str(count).zfill(1) + ".jpg", cropImg)       # 保存
        count+=1
path = "/home/lid/share/cannypicture/image"  
cutPicture()    
#ChangeFileName(path + "/image","jpg","") # 你的路径, 前缀,后缀
#ChangeFileName(path + "/com","jpg","")
#ChangeFileName(path + "/banzi","banzi","")



最终效果

微信截图_20201110095728.png

sitemap