Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # $VER: picrenamer.py v1.00 (05-Jun-2024) by Zeb/Slipstream
- from sys import argv
- from glob import glob
- from pathlib import Path
- from PIL import Image
- from os import rename
- #default values
- strDirName="."
- blnCommit=False
- strAdd=""
- def checkSlash(str):
- l=len(str)-1
- if str[l]=="/":
- return True
- else:
- return False
- intCount=0
- #parse the arguments on the command line
- intArgC=len(argv)
- if intArgC>1: #if no arguments use default values set at the top
- for i in range(1,intArgC):
- if argv[i]=="-d": #check for a directory name
- strDirName=argv[i+1]
- i+=1
- if argv[i]=="commit": #check for commit
- blnCommit=True
- if argv[i]=="-s": #check for a string to add
- strAdd=argv[i+1]
- i+=1
- if not checkSlash(strDirName):
- strDirName=strDirName+"/"
- print(f"Dir Name: {strDirName}")
- lstFiles=glob(strDirName+"*.[j|J][p|P][g|G]")
- lstFiles.sort()
- for i in lstFiles:
- im=Image.open(i)
- tmp=im.getexif()
- dt=tmp[306].split(" ") #extract the date time into a list
- tm=dt[1].split(":") #extract the date into a list
- dt=dt[0].split(":") #extract the time into a list
- strAfter=f"{strDirName}{dt[0]}-{dt[1]}-{dt[2]} {tm[0]}-{tm[1]}-{tm[2]}"
- if len(strAdd)>0:
- strAfter=strAfter+" "+strAdd
- strAfter=strAfter+".jpg"
- file=Path(strAfter)
- if blnCommit:
- if file.exists():
- print(f"ERR: File '{strAfter}' exists ({i})")
- else:
- #print(f"Rename '{i}' to '{strAfter}'")
- rename(i,strAfter)
- intCount+=1
- else:
- print(f">'{i}' to '{strAfter}'")
- if blnCommit:
- print(f"Files: {intCount}/{len(lstFiles)}")
- else:
- print(f"Files: {len(lstFiles)} (nothing has been changed)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement