python的N个小功能(找到符合要求的图片,重命名,改格式,缩放,进行随机分配)

##########################################################################

循环读取该目录下所有子目录和子文件

#########################################################################

import os

import os.path

 

def directory(rootdir):                                  # 指明被遍历的文件夹

         for parent,dirnames,filenames in os.walk(rootdir):   #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字

                

             for dirname in  dirnames:                        #输出文件夹信息

                 print "parent is:" + parent

                 print "dirname is:" + dirname

 

             for filename in filenames:                      

                 print "parent is:" + parent                  #输出文件夹信息

                 print "filename is:" + filename              #输出文件信息

                 print "the full name of the file is:" + os.path.join(parent,filename)  #输出文件路径信息

 

directory('G:\\test')

 

###############################################################################

保存图片//修改图片长宽比情况

###############################################################################

from PIL import Image

 

def modifysize(infile,outfile):    #输入文件,输出文件  

         im = Image.open(infile)

         (x,y) = im.size                #read image size,读取文件大小

         x_s = 600                                         #define standard width,定义标准

         y_s = 600                                              #calc height based on standard width

         out = im.resize((x_s,y_s),Image.ANTIALIAS)                   #resize image with high-quality

         out.save(outfile)

 

modifysize(' G:\\test\\one.jpg ', ' G:\\TEST\\one.jpg ')

 

from PIL import Image

def save(infile,outfile):    #输入文件,输出文件  

         im = Image.open(infile)

         (x,y) = im.size                #read image size,读取文件大小

         x_s = x                                                   #define standard width,定义标准

         y_s = y                                         #calc height based on standard width

         out = im.resize((x_s,y_s),Image.ANTIALIAS)                   #resize image with high-quality

         out.save(outfile)

 

########################################################################

对文件夹中所有文件重命名函数

########################################################################

####################原来函数############################################

import os

def rename():

         path=r'G:\TEST'

         filelist=os.listdir(path)#该文件夹下所有的文件(包括文件夹)

         for files in filelist:#遍历所有文件

                   Olddir=os.path.join(path,files);#原来的文件路径

                   if os.path.isdir(Olddir):#如果是文件夹则跳过

                            continue;

                   filename=os.path.splitext(files)[0];#文件名

                   filetype=os.path.splitext(files)[1];#文件扩展名

                   Newdir=os.path.join(path,filename[0]+filetype);#新的文件路径

                   os.rename(Olddir,Newdir);#重命名

 

rename()

#####################修改后函数###########################################

 

import os

def rename(path,foldername):         #传入路径和文件夹名

         if len(foldername)!=5 and not(foldername.isdigit()):           #传入文件夹名是五位数的且字符串全为数值,不符合跳过

                   return

         filelist=os.listdir(path)#该文件夹下所有的文件(包括文件夹)

         i=0

         for files in filelist:#遍历所有文件

                   Olddir=os.path.join(path,files);#原来的文件路径

                   if os.path.isdir(Olddir):#如果是文件夹则跳过

                            continue;

                   filename=os.path.splitext(files)[0];#文件名

                   filetype=os.path.splitext(files)[1].lower();#文件扩展名

                   if filetype!='.jpg':

                            filetype='.jpg'

 

                   number='%d' %i

                   Newdir=os.path.join(path,foldername+number+filetype);#新的文件路径,新名称命名规则

                   os.rename(Olddir,Newdir);#重命名

                   i=i+1

 

rename('G:\\TEST\\20010','20010')

 

 

###############################################################

#########################################################################

实现查找__过滤掉不需要的__修改大小写功能(每个文件一行一行读取)

#########################################################################

# -*- coding: utf-8 -*-

"""

Created on Fri Feb 17 20:25:05 2017

 

@author: who

"""

 

import chardet

import os

import os.path

import re

import random

import string

 

rootdir=r'G:\TEST'

 

from PIL import Image

 

################保存图片,从一个文件夹到另一个文件夹##############

def save(infile,outfile):    #输入文件,输出文件  

         im = Image.open(infile)

         (x,y) = im.size                #read image size,读取文件大小

         x_s = x                                                   #define standard width,定义标准

         y_s = y                                         #calc height based on standard width

         out = im.resize((x_s,y_s),Image.ANTIALIAS)                   #resize image with high-quality

         out.save(outfile)

 

################过滤掉不需要的,修改大小写功能#####################

for parent, dirnames, filenames in os.walk(rootdir):   # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字

 

 

         try:

                   for filename in filenames:

 

                            filenamepre=os.path.splitext(filename.decode("gbk"))[0];#文件名前缀

                            filetype=os.path.splitext(filename.decode("gbk"))[1].lower();#文件扩展名

 

                            pswpath = os.path.join(parent, filename.decode("gbk"));

                            tmppath = os.path.join(r'G:\test',filename.decode("gbk")) #写到另一个文件夹#

                            if filetype=='XXX':                      ####符合类型的文件####

                                    

                                     tmp_file = open(tmppath, "w")

                                     with open(pswpath) as f:

                                         lines = f.readlines()            

 

                                         for line in lines:                  ####一行一行读取 ####  

                                                   if line.find('aaa') > -1:         ####过滤掉含有aaa的这行####

                                                       continue

                                                   if line.find('CCC')>-1:          ####将含有CCC这行小写化####

                                                            tmp_file.write(line.lower())

                                                   else:

                                                            tmp_file.write(line)

                                      

                                     tmp_file.close()

 

                                    

                            else:

                                     save(pswpath,tmppath);

         except IOError:

                   pass

 

#############################################################################

将所有文件所有文件名记录下来,进行随机分配

############################################################################

# -*- coding: utf-8 -*-

"""

Created on Fri Feb 17 15:53:34 2017

 

@author: who

"""

 

# -*- coding: utf-8 -*-

"""

Created on Wed Feb 15 18:07:03 2017

 

@author: Administrator

"""

 

import chardet

import os

import os.path

import re

import random

import string

 

from PIL import Image

 

rootdir=r'G:\TEST';    # 指明被遍历的文件夹

 

dict={};

dictfileold={};

dictfileoldfile={};

dictparent={};

dir={}

i=0;

 

for parent,dirnames,filenames in os.walk(rootdir):         #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字

         j=0

         try:          

                   for filename in filenames:                         #输出文件信息

                            filenamepre=os.path.splitext(filename)[0];     #文件名前缀

                            filetype=os.path.splitext(filename)[1].lower();#文件扩展名

                            tmpstr=parent.rsplit('\\')

 

                            fulloldname=os.path.join(parent,filename);

                            dict[filenamepre]=i;                           #旧名称为key

                            dictfileold[i]=fulloldname;                    #旧路径名称为value  

                            dictfileoldfile[i]=filenamepre;                #旧文件名为value                      

                            dictparent[i]=parent;

                            dir[i]=tmpstr[-1]                              #得到上一级目录

                            i=i+1;

                            j=j+1;

         except IOError:

                   pass

 

##############文件数,进行随机分配##########################################

foldernum=i;                                             

randomnum=range(foldernum);

random.shuffle(randomnum);

trainnum=int(foldernum*0.7);

 

train=open(r'G:\train.txt','w')

val=open(r'G:\val.txt','w')

trainval=open(r'G:\trainval.txt','w')

 

for j in range(0,foldernum):

         trainval.write(dictfileoldfile[j])

         trainval.write('\n')

         if j in randomnum[0:trainnum]:

                   train.write(dictfileoldfile[j])

                   train.write('\n')

         else:

                   val.write(dictfileoldfile[j])

                   val.write('\n')

 

train.close()

val.close()

trainval.close()

 

############################################################################

找到符合要求的文件,进行随机分配

  • 编号要以6为数字命名,例如000034.jpg
  • 图片要是JPEG/JPG格式的,PNG之类的需要自己转换下
  • 图片的长宽比(width/height)要在0.462-6.828之间,就是太过瘦长的图片不要

############################################################################

# -*- coding: utf-8 -*-

"""

Created on Wed Feb 15 18:07:03 2017

 

@author: Administrator

"""

 

import chardet

import os

import os.path

import re

import random

import string

 

from PIL import Image

 

def save(infile,outfile):    #输入文件,输出文件  

         im = Image.open(infile)

         (x,y) = im.size                #read image size,读取文件大小

         x_s = x                                                   #define standard width,定义标准

         y_s = y                                         #calc height based on standard width

         out = im.resize((x_s,y_s),Image.ANTIALIAS)                   #resize image with high-quality

         out.save(outfile)

 

 

def modifysize(infile,outfile):    #输入文件,输出文件

         try:                           #过滤掉任意的异常

                   im = Image.open(infile)

         except :

                   return 0

 

         (x,y) = im.size                #read image size,读取文件大小

         x_s = 800                                         #define standard width,定义标准

         y_s = 600

         try:                                            #过滤掉任意的异常

             out = im.resize((x_s,y_s),Image.ANTIALIAS)                  

         except :

             return 0

         out.save(outfile)

         return 1

        

rootdir=r'E:\TEST';             # 指明被遍历的文件夹

 

dict={};

dictfilenew={};

dictfileold={};

dictfilenewname={};

dictparent={};

dir={}

i=0;

 

for parent,dirnames,filenames in os.walk(rootdir):         #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字

         j=0

         try:          

                   for filename in filenames:                                      #输出文件信息

                            if((re.search(u'XXX',filename.decode("gbk"))==None):   #找到符合要求文件名

                                     continue;

                            filenamepre=os.path.splitext(filename.decode("gbk"))[0];#文件名前缀

                            filetype=os.path.splitext(filename.decode("gbk"))[1].lower();#文件扩展名

                            if (filetype!='.jpg' and filetype!='.png'):        ##符合相应后缀的图片

                                     continue;

                            tmpstr=parent.rsplit('\\')

                            print u"上层目录:"+tmpstr[-1]

 

                            if len(tmpstr[-1])!=5 or not(tmpstr[-1].isdigit()):           #传入文件夹名是五位数的且字符串全为数值,不符合跳过

                                     continue

                            fullname=os.path.join(parent,filename.decode("gbk"));

 

                            if filetype!='.jpg':

                                     filetype='.jpg'

 

 

                            number='%d' %j

                            Newname=tmpstr[-1]+number+filetype

                            Newdir=os.path.join(parent,tmpstr[-1]+number+filetype);              #新的文件路径,新名称命名规则

 

                            fullnewname=os.path.join(parent,Newname.decode("gbk"));       #将所有新路径名称,旧路径名称,新名称,旧名称,上一级目录都用字典保存

                            fulloldname=os.path.join(parent,filename.decode("gbk"));

                            dict[fullnewname]=i;        #新路径名称为key                         

                            dictfilenew[i]=fullnewname;  #新路径名称为value

                            dictfileold[i]=fulloldname;   #旧路径名称为value                         

                            dictfilenewname[i]=Newname.decode("gbk");  #新名称为value

                            dictparent[i]=parent;

                            dir[i]=tmpstr[-1]

                            i=i+1;

                            j=j+1;

         except IOError:

                   pass

 

###########################将所有文件随机分配##################################

foldernum=i;

randomnum=range(foldernum);

random.shuffle(randomnum);

trainnum=int(foldernum*0.8);

 

for j in range(0,foldernum):

         if j in randomnum[0:trainnum]:

                   outfile=os.path.join(r'E:\train',dictfilenewname[j])

                   modifysize(dictfileold[j],outfile)

         else:

                   outfile=os.path.join(r'E:\test',dictfilenewname[j])

                   modifysize(dictfileold[j],outfile)

posted @ 2017-02-20 14:31  白鹭倾城  阅读(3119)  评论(0编辑  收藏  举报