Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf8 -*-
- # Copyright © 2021 Benoît Boudaud <https://miamondo.org/contact>
- # This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
- # License as published by the Free Software Foundation, either version 3 of the License, or any later version.
- # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
- # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
- # You should have received a copy of the GNU General Public License along with this program.
- # If not, see <http://www.gnu.org/licenses/>
- # ----------------------------------------------------------------------------------------------------------------------
- # This script takes one argument which is the absolute path of any directory. So, if you want to run the
- # script, just enter this command in a terminal for instance: /home/$USER/rename_pictures.py /home/$USER/Images
- # Part n° 1: if "Images" is in the path, the script renames every pictures according to this model: "directory-n.ext"
- # For example, if you have a pictures directory named cats, you will get: cats-1.png, cats-2.jpg, cats-3.png, etc...
- # Part n° 2: Every pictures are converted in PNG.
- # Part n° 3: Every pictures that are not in PNG-format are deleted.
- # ---- MODULES ---------------------------------------------------------------------------------------------------------
- import os
- import sys
- import subprocess
- # ---- 1. Renaming the pictures in the Images directory ----------------------------------------------------------------
- if sys.argv[2] == "rename" and "Images" in sys.argv[1]:
- for root, dirs, files in os.walk(sys.argv[1]):
- for i, f_ in enumerate(files):
- if f"{os.path.basename(root)}-{i+1}{os.path.splitext(f_)[1]}" not in files:
- os.rename(os.path.join(root, f_), os.path.join(root,
- f"{os.path.basename(root)}-{i+1}{os.path.splitext(f_)[1]}"))
- # ---- 2. Converting every pictures in PNG -----------------------------------------------------------------------------
- if sys.argv[2] == "convert":
- for root, dirs, files in os.walk(sys.argv[1]):
- for f_ in files:
- subprocess.run(["convert", os.path.join(root, f_), os.path.join(root, f'{os.path.splitext(f_)[0]}.png')])
- # ---- 3. Deleting pictures that are not in PNG-format -----------------------------------------------------------------
- if sys.argv[2] == "delete":
- for root, dirs, files in os.walk(sys.argv[1]):
- for f_ in files:
- if not f_.endswith(".png"):
- subprocess.run(["rm", os.path.join(root, f_)])
Add Comment
Please, Sign In to add comment