-
Notifications
You must be signed in to change notification settings - Fork 19
/
cropall.py
executable file
·113 lines (94 loc) · 3.81 KB
/
cropall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#! /usr/bin/env python
#
# cropall: a tiny batch image processing app to crop pictures in less clicks
#
# Copyright (C) 2015-2024 Pyarelal Knowles
#
# 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
# (at your option) 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/>.
import os
import sys
import error_handler
import argparse
import configparser
import pathlib
import shutil
logger = error_handler.activate("cropall")
default_config_file = pathlib.Path("cropall_default.ini")
config_file = pathlib.Path("cropall.ini")
# dependencies are in a subdirectory when packaged with pyinstaller
if hasattr(sys, "_MEIPASS"):
internal_dir = pathlib.Path(sys._MEIPASS)
default_config_file = internal_dir / pathlib.Path("cropall_default.ini")
config_file = internal_dir / config_file
# ImageMagick directories
os.environ["MAGICK_HOME"] = str(internal_dir)
os.environ["MAGICK_CODER_FILTER_PATH"] = str(internal_dir / "modules/filters")
os.environ["MAGICK_CODER_MODULE_PATH"] = str(internal_dir / "modules/coders")
# Add the above paths PATH for wand/imagemagick on windows
if sys.platform == "win32":
os.environ["PATH"] += os.pathsep + os.environ["MAGICK_HOME"]
os.environ["PATH"] += os.pathsep + os.environ["MAGICK_CODER_FILTER_PATH"]
os.environ["PATH"] += os.pathsep + os.environ["MAGICK_CODER_MODULE_PATH"]
config = configparser.ConfigParser()
config.read(default_config_file)
if os.path.exists(config_file):
config.read(config_file)
parser = argparse.ArgumentParser()
parser.add_argument(
"input_folder",
type=pathlib.Path,
nargs="?",
help="Directories for source photos",
)
def getImages(config, dir):
logger.info("Scanning {}".format(dir))
extensions = config["image_extensions"].split()
images = []
for filename in os.listdir(dir):
basename, ext = os.path.splitext(filename)
if ext.lower() in extensions:
logger.info(" Found {}".format(filename))
images += [filename]
logger.info("Found {} images".format(len(images)))
return images
if __name__ == "__main__":
cropall_config = config["cropall"]
cropall_config["first_run"] = "False"
args = parser.parse_args()
if args.input_folder:
input_folder = str(args.input_folder)
else:
# Ask for the input directory
import tkinter.filedialog
input_folder = tkinter.filedialog.askdirectory(
initialdir=cropall_config["input_folder"], title="Please select a directory"
)
if not len(input_folder):
raise ValueError("No directory selected. Exiting.")
input_folder = pathlib.Path(os.path.normpath(input_folder))
images = getImages(cropall_config, input_folder)
if not len(images):
raise SystemExit("No images found in '{}'. Exiting.".format(input_folder))
cropall_config["input_folder"] = str(input_folder)
output_folder = input_folder / pathlib.Path(cropall_config["output_folder"])
if not os.path.exists(output_folder):
logger.info("Creating output directory, '{}'".format(output_folder))
os.makedirs(output_folder)
import cropper
cropper = cropper.Cropper(config)
import gui
app = gui.App(config, cropper, input_folder, images, output_folder)
app.mainloop()
with open(config_file, "w") as filehandle:
config.write(filehandle)