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()
  • 相关阅读:
    JSON
    邮箱正则表达式
    聚聚科技---PHP开发笔试题及答案
    PHP字符串左边补0,字符串右边补0
    CSS3实现带阴影的弹球
    背景颜色渐变
    CSS3---滤镜
    CSS3裁剪与遮罩解析
    CSS3---混合模式
    使用CSS3制作各种形状
  • 原文地址:https://www.cnblogs.com/dudu1992/p/8964792.html
Copyright © 2011-2022 走看看