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()
  • 相关阅读:
    ReadOnly TextEdit输入问题
    关于正太分布单侧区间上下限的定义
    关于Devexpress richEditControl
    CentOS7 升级 cmake
    极客时间实战目录
    kafka安装
    查找连续相同值的算法,并给出连续相同值的个数以及位置
    解决若依linux启动ERROR
    supervisor配置进程
    python做上位机
  • 原文地址:https://www.cnblogs.com/dudu1992/p/8964792.html
Copyright © 2011-2022 走看看