zoukankan      html  css  js  c++  java
  • 使用Python对图片进行统一命名,统一格式处理。

    from PIL import Image
    import os.path
    def convertjpg(jpgfile,outdir,width=100,height=100):
         img = Image.open('/Users/dudu/Desktop/catclass/cat/car2/sl/'+jpgfile)
         try:
             new_img = img.resize((width, height), Image.BILINEAR)
             new_img.save(os.path.join(outdir, os.path.basename(jpgfile)))
         except Exception as e:
             print(e)
    for jpgfile in os.listdir('/Users/dudu/Desktop/catclass/cat/car2/sl/'):
        print(jpgfile)
        convertjpg(jpgfile, "/Users/dudu/Desktop/catclass/cat/car2/sl/")
    
    #统一图片类型
    def ranamesJPG(filepath, kind):
        images = os.listdir(filepath)
        for name in images:
             os.rename(filepath+name, filepath+kind+'_'+name.split('.')[0]+'.jpg')
             print(name)
             print(name.split('.')[0])
    
    ranamesJPG('/Users/dudu/Desktop/catclass/data2/','car_c')
    
    
    import os
    
    class BatchRename():
        def __init__(self):
            self.path = '/Users/dudu/Desktop/catclass/data2/'
    
        def rename(self):
            filelist = os.listdir(self.path)
            total_num = len(filelist)
            i = 1001
            for item in filelist:
                if item.endswith('.jpg'):
                    src = os.path.join(os.path.abspath(self.path), item)
                    dst = os.path.join(os.path.abspath(self.path), str(i) + '.jpg')
                    try:
                        os.rename(src, dst)
                        print ('converting %s to %s ...' % (src, dst))
                        i = i + 1
                    except:
                        continue
            print ('total %d to rename & converted %d jpgs' % (total_num, i))
    
    if __name__ == '__main__':
        demo = BatchRename()
        demo.rename()
  • 相关阅读:
    centos8.0 安装 jenkins
    No match for argument: mysql-community-server Error: Unable to find a match: mysql-community-server
    Navicat 远程连接 centos8.0
    centos8.0 安装 mysql
    -bash: java-version: command not found
    centos8.0 安装 JDK1.8
    [Err] 1062
    android之Fragment基础详解(一)
    Android之RecyclerView(一)
    Android之ProgressBar
  • 原文地址:https://www.cnblogs.com/dudu1992/p/8964792.html
Copyright © 2011-2022 走看看