Advertisement
Yesideez

PicRenamer.py

Jun 5th, 2024 (edited)
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | Source Code | 0 0
  1. # $VER: picrenamer.py v1.00 (05-Jun-2024) by Zeb/Slipstream
  2.  
  3. from sys import argv
  4. from glob import glob
  5. from pathlib import Path
  6. from PIL import Image
  7. from os import rename
  8.  
  9. #default values
  10. strDirName="."
  11. blnCommit=False
  12. strAdd=""
  13.  
  14. def checkSlash(str):
  15.   l=len(str)-1
  16.   if str[l]=="/":
  17.     return True
  18.   else:
  19.     return False
  20.  
  21. intCount=0
  22.  
  23. #parse the arguments on the command line
  24. intArgC=len(argv)
  25. if intArgC>1: #if no arguments use default values set at the top
  26.   for i in range(1,intArgC):
  27.     if argv[i]=="-d": #check for a directory name
  28.       strDirName=argv[i+1]
  29.       i+=1
  30.     if argv[i]=="commit": #check for commit
  31.       blnCommit=True
  32.     if argv[i]=="-s": #check for a string to add
  33.       strAdd=argv[i+1]
  34.       i+=1
  35.  
  36. if not checkSlash(strDirName):
  37.   strDirName=strDirName+"/"
  38. print(f"Dir Name: {strDirName}")
  39. lstFiles=glob(strDirName+"*.[j|J][p|P][g|G]")
  40. lstFiles.sort()
  41.  
  42. for i in lstFiles:
  43.   im=Image.open(i)
  44.   tmp=im.getexif()
  45.   dt=tmp[306].split(" ") #extract the date time into a list
  46.   tm=dt[1].split(":")    #extract the date into a list
  47.   dt=dt[0].split(":")    #extract the time into a list
  48.   strAfter=f"{strDirName}{dt[0]}-{dt[1]}-{dt[2]} {tm[0]}-{tm[1]}-{tm[2]}"
  49.   if len(strAdd)>0:
  50.     strAfter=strAfter+" "+strAdd
  51.   strAfter=strAfter+".jpg"
  52.   file=Path(strAfter)
  53.   if blnCommit:
  54.     if file.exists():
  55.       print(f"ERR: File '{strAfter}' exists ({i})")
  56.     else:
  57.       #print(f"Rename '{i}' to '{strAfter}'")
  58.       rename(i,strAfter)
  59.       intCount+=1
  60.   else:
  61.     print(f">'{i}' to '{strAfter}'")
  62.  
  63. if blnCommit:
  64.   print(f"Files: {intCount}/{len(lstFiles)}")
  65. else:
  66.   print(f"Files: {len(lstFiles)} (nothing has been changed)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement