• Python 修改图片文件的名称为拍摄时间


    修改图片文件的名称:拍摄时间

    import os
    import exifread
    import time
    import os.path 
    
    
    path = "D:\\新建文件夹 (2)\\"
    path2 = "D:\\异常\\"
    
    
    file_list = os.listdir(path)
    
    for index in range(0, len(file_list)):
    	old_filename = file_list[index]
    	old_fullname = path + old_filename
    	tags = ""
    	
    	print(old_filename)
    	with open(old_fullname,"rb") as jpegFile:
    		tags = exifread.process_file(jpegFile)
    		
    	if 'EXIF DateTimeOriginal' in tags:
    		str_time = str(tags['EXIF DateTimeOriginal'])
    		timeStruct = ''
    		#print(str_time)
    		
    		try:
    			if '上午' in str_time:
    				timeStruct = time.strptime(str_time, "%Y:%m:%d %H:%M:%S上午")
    			elif '下午' in str_time:
    				timeStruct = time.strptime(str_time, "%Y:%m:%d %H:%M:%S下午")
    			else:
    				timeStruct = time.strptime(str_time, "%Y:%m:%d %H:%M:%S")
    			str_time = time.strftime("%Y%m%d_%H%M%S", timeStruct)
    		except ValueError as e:
    			print('ValueError:',old_filename,e)
    			
    		new_filename = str_time + os.path.splitext(old_fullname)[1]
    		new_fullname = path + new_filename
    		if old_filename != new_filename:
    			try:
    				os.rename(old_fullname, new_fullname)
    			except IOError as e:
    				print('IOError:',old_filename,e)
    				os.rename(old_fullname, path2+old_filename)
    				
    	else:
    		str_time = str(time.strftime("%Y%m%d_%H%M%S", time.localtime(os.path.getmtime(old_fullname))))
    		new_filename = str_time + os.path.splitext(old_fullname)[1]
    		new_fullname = os.path.join(path, new_filename)
    		if old_filename != new_filename:
    			try:
    				os.rename(old_fullname, new_fullname)
    			except IOError as e:
    				print('IOError:',old_filename,e)
    				os.rename(old_fullname, path2+old_filename)
    

    修改图片文件的名称:拍摄时间+原文件名

    import os
    import exifread
    import time
    import os.path 
    
    
    path = "D:\\新建文件夹 (2)\\"
    path2 = "D:\\异常\\"
    
    
    file_list = os.listdir(path)
    
    for index in range(0, len(file_list)):
    	old_filename = file_list[index]
    	# 用于拍摄日期重名 比较特殊,最终文件名为:拍摄日期+创建时间
    	old_filename2 = "__" + os.path.splitext(old_filename)[0]
    	old_fullname = path + old_filename
    	
    	tags = ""
    	
    	print(old_filename)
    	with open(old_fullname,"rb") as jpegFile:
    		tags = exifread.process_file(jpegFile)
    		
    	if 'EXIF DateTimeOriginal' in tags:
    		str_time = str(tags['EXIF DateTimeOriginal'])
    		timeStruct = ''
    		#print(str_time)
    		
    		try:
    			if '上午' in str_time:
    				timeStruct = time.strptime(str_time, "%Y:%m:%d %H:%M:%S上午")
    			elif '下午' in str_time:
    				timeStruct = time.strptime(str_time, "%Y:%m:%d %H:%M:%S下午")
    			else:
    				timeStruct = time.strptime(str_time, "%Y:%m:%d %H:%M:%S")
    			str_time = time.strftime("%Y%m%d_%H%M%S", timeStruct)
    		except ValueError as e:
    			print('ValueError:',old_filename,e)
    			
    		new_filename = str_time + old_filename2 + os.path.splitext(old_fullname)[1]
    		new_fullname = path + new_filename
    		if old_filename != new_filename:
    			try:
    				os.rename(old_fullname, new_fullname)
    			except IOError as e:
    				print('IOError:',old_filename,e)
    				os.rename(old_fullname, path2+old_filename)
    				
    	else:
    		str_time = str(time.strftime("%Y%m%d_%H%M%S", time.localtime(os.path.getmtime(old_fullname))))
    		new_filename = str_time + old_filename2 + os.path.splitext(old_fullname)[1]
    		new_fullname = os.path.join(path, new_filename)
    		if old_filename != new_filename:
    			try:
    				os.rename(old_fullname, new_fullname)
    			except IOError as e:
    				print('IOError:',old_filename,e)
    				os.rename(old_fullname, path2+old_filename)
    

    修改图片文件的名称:创建时间

    using System;
    using System.IO;
    
    namespace ConsoleApp1
    {
        public class Class14
        {
            public void test1()
            {
                string exeName =AppDomain.CurrentDomain.FriendlyName;
                string path = Directory.GetCurrentDirectory();            
                DirectoryInfo dir = new DirectoryInfo(path); 
                FileInfo[] inf = dir.GetFiles();
    
                foreach (FileInfo fileInfo in inf)
                {
                    Console.WriteLine(fileInfo.FullName);
                    if (fileInfo.Name.Equals(exeName))
                    {
                        continue;
                    }
                    string newPath = "";
                    string ext = Path.GetExtension(fileInfo.FullName);
                    string newName = fileInfo.CreationTime.ToString("yyyyMMdd_HHmmss_ffffff");
                    newPath = $"{fileInfo.DirectoryName}\\{newName}{ext}";
                    fileInfo.MoveTo(newPath);
                }
            }
        }
    }
    
    
  • 相关阅读:
    CentOS挂载ntfs
    搬水果
    CentOS安装完显卡启动进不去系统
    二叉树遍历
    拓展的异常类
    Visual Studio 2008 IDE 出现 “帮助更新正在进行”错误的解决办法
    JSON工具SuperObject编译时"整数溢出"异常的解决办法
    引用命名区域
    ruby操作excel的几段代码
    Delphi2009&2007中,测试工程的初级使用(DUnit)
  • 原文地址:https://www.cnblogs.com/guxingy/p/16350681.html
Copyright © 2020-2023  润新知