miamondo

rename_pictures.py

Jan 7th, 2022 (edited)
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf8 -*-
  3.  
  4. # Copyright © 2021 Benoît Boudaud <https://miamondo.org/contact>
  5. # This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  6. # License as published by the Free Software Foundation, either version 3 of the License, or any later version.
  7. # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  8. # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. # You should have received a copy of the GNU General Public License along with this program.
  10. # If not, see <http://www.gnu.org/licenses/>
  11.  
  12. # ----------------------------------------------------------------------------------------------------------------------
  13.  
  14. # This script takes one argument which is the absolute path of any directory. So, if you want to run the
  15. # script, just enter this command in a terminal for instance: /home/$USER/rename_pictures.py /home/$USER/Images
  16.  
  17. # Part n° 1: if "Images" is in the path, the script renames every pictures according to this model: "directory-n.ext"
  18. # For example, if you have a pictures directory named cats, you will get: cats-1.png, cats-2.jpg, cats-3.png, etc...
  19.  
  20. # Part n° 2: Every pictures are converted in PNG.
  21.  
  22. # Part n° 3: Every pictures that are not in PNG-format are deleted.
  23.  
  24. # ---- MODULES ---------------------------------------------------------------------------------------------------------
  25.  
  26. import os
  27. import sys
  28. import subprocess
  29.  
  30. # ---- 1. Renaming the pictures in the Images directory ----------------------------------------------------------------
  31.  
  32. if sys.argv[2] == "rename" and "Images" in sys.argv[1]:
  33.     for root, dirs, files in os.walk(sys.argv[1]):
  34.         for i, f_ in enumerate(files):
  35.             if f"{os.path.basename(root)}-{i+1}{os.path.splitext(f_)[1]}" not in files:          
  36.                 os.rename(os.path.join(root, f_), os.path.join(root,
  37.                     f"{os.path.basename(root)}-{i+1}{os.path.splitext(f_)[1]}"))
  38.                    
  39. # ---- 2. Converting every pictures in PNG -----------------------------------------------------------------------------
  40.  
  41. if sys.argv[2] == "convert":        
  42.     for root, dirs, files in os.walk(sys.argv[1]):
  43.         for f_ in files:
  44.             subprocess.run(["convert", os.path.join(root, f_), os.path.join(root, f'{os.path.splitext(f_)[0]}.png')])
  45.  
  46. # ---- 3. Deleting pictures that are not in PNG-format -----------------------------------------------------------------
  47.  
  48. if sys.argv[2] == "delete":
  49.     for root, dirs, files in os.walk(sys.argv[1]):
  50.         for f_ in files:            
  51.             if not f_.endswith(".png"):
  52.                 subprocess.run(["rm", os.path.join(root, f_)])
  53.    
  54.  
Add Comment
Please, Sign In to add comment