-
Notifications
You must be signed in to change notification settings - Fork 1
/
batch_image_manager.py
241 lines (203 loc) · 10.7 KB
/
batch_image_manager.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# Copyright (C) <2017> <Akshat Singh>
#
# 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/>.
"""Script to run a compression only on specific camera make but not on others"""
from __future__ import print_function
import os
import time
import errno
import shutil
import logging
import subprocess
from datetime import datetime
import argparse
from argparse import RawTextHelpFormatter
# Command line argument validation functions...
def is_valid_directory(parser, arg):
"Function for checking specfied directory exists or not."
if not os.path.isdir(arg):
parser.error('\n\n\tThe directory {} does not exist!'.format(arg))
return arg
def is_target_directory(parser, arg):
"Function for checking specfied directory exists or not."
if not os.path.isdir(arg):
print('\n\n\tThe directory %s does not exist!, so creating it for you..' % arg)
try:
subprocess.call(['mkdir', '-p', arg])
return arg
except IOError as ioexp:
parser.error('\n\n\tThe directory {} does not exist and unable to create for you, \n\
\r\tplease create it manually!'.format(arg), ioexp)
return arg
def is_valid_option(parser, arg):
"Function for checking option is on or off."
if not arg in ('on', 'off'):
parser.error('\n\n\t{} is not a valid input for turning option on or off! \n\
\r\tPlease specify \"on\" for turning the option on and \"off\" \n\
\r\tfor turning it off.'.format(arg))
return arg
def is_valid_logging_status(parser, arg):
"Function for checking logging status is valid or not."
if not arg in ('on', 'off'):
parser.error('\n\n\t{} is not a valid input for turning logging on/off! Please specify \n\
\r\t\"on\" for turning logging on and \"off\" for turning logging off. \n'
.format(arg))
return arg
## =========> Command line arguments parsing -- starts <========= ##
PARSER = argparse.ArgumentParser(description='[Purpose - This script is useful in a situation \
where we want to run a compression only on specific camera make but not on others and later want \
to do some manual image processing using tools like gimp on some camera make images and also at \
the same time we also want to maintain the sequence of the images based on their capturing time \
not based on the image modification.]\
\n\n\n**********************************************************************\n\
This script will copy the images from the specified directory and all of its sub-directory to the \
target directory. The target directory will be the provided on the command line argument; but the \
final destination will be targetDir/CameraManufacurer__CameraModel/unixTimeStamp_CameraMake.JPG\
\n\n**********************************************************************\nBatch Image Manager \
is an advaced version of the Batch Image Compression utility. For running this program you need \
to have exif tool and imagemagick installed on your machine. This program is used for mixing the \
various images taken from different camera make based on their capturing time in ascending order \
and rename each image on their unix timestamp.\n\nIf compression option is selected as OFF then \
this script wil not create any sub-directories based on camera make just it will arrange the \
pictures based on their capturing time. \n\n\n\t', formatter_class=RawTextHelpFormatter)
PARSER.add_argument('-i', '--info', help='Information about the Camera make and Model',
metavar='<Camera Information>')
PARSER.add_argument('-s', '--source_directory', help='Directory to read input files',
required=True, metavar='<Source Directory>',
type=lambda x: is_valid_directory(PARSER, x))
PARSER.add_argument('-t', '--target_directory', help='Directory to save output files.',
required=True, metavar='<Target Directory>',
type=lambda x: is_target_directory(PARSER, x))
PARSER.add_argument('-cmp', '--compression', help='Compression On/Off', required=True,
metavar='<Compression on/off>', type=lambda x: is_valid_option(PARSER, x))
PARSER.add_argument('-clq', '--compressionQuality', help='Quality of the Image to retain for \n\
specific Camera make [Image Quality range is 1-100]',
metavar='<CameraMake_#ImageQuality Eg.: Canon100D_#90>')
PARSER.add_argument('-l', '--log_file', help='Path of the log file.', metavar='<Log File>')
PARSER.add_argument('-ls', '--logging_onoff', help='Logging status On/Off',
metavar='<Logging on/off>', type=lambda x: is_valid_logging_status(PARSER, x))
ARGS = PARSER.parse_args()
## =========> Command line arguments parsing -- ends <========= ##
subprocess.call('clear')
## =========> Logging Configurations -- starts <========= ##
LOGGER_FILE = ARGS.log_file
LOGGING_STATUS = ARGS.logging_onoff
if not LOGGER_FILE:
LOG_FILE = '/tmp/BatchImageManager.log'
else:
LOG_FILE = LOGGER_FILE + ".log"
# create logger
LOGGER = logging.getLogger('BIMamager')
LOGGER.setLevel(logging.DEBUG)
# Turning logging on or off
if LOGGING_STATUS:
LOGGER.disabled = bool(LOGGING_STATUS == 'off')
else:
LOGGER.disabled = False
# add a file handler
FILE_HANDLER = logging.FileHandler(LOG_FILE)
FILE_HANDLER.setLevel(logging.DEBUG)
# create console handler and set level to debug
CONSOLE_HANDLER = logging.StreamHandler()
CONSOLE_HANDLER.setLevel(logging.DEBUG)
# create formatter
FORMATTER = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s', \
datefmt='%m/%d/%Y %I:%M:%S %p')
# add formatter to handlers
FILE_HANDLER.setFormatter(FORMATTER)
CONSOLE_HANDLER.setFormatter(FORMATTER)
# add ch to logger
LOGGER.addHandler(FILE_HANDLER)
LOGGER.addHandler(CONSOLE_HANDLER)
## =========> Logging Configurations -- ends <========= ##
INFO = ARGS.info
SOURCE_DIR = ARGS.source_directory
TARGET_DIR = ARGS.target_directory
COMPRESSION = ARGS.compression
COMP_QUALITY = ARGS.compressionQuality
TMP_FILE = '/tmp/batchIMv2.txt'
DEVNULL = open('/dev/null', 'w')
def timestamp_query(image_name):
'''Function for returning the Image capturing time unix timestamp'''
exif_cmd = subprocess.Popen(['exif', '-x', image_name], stdout=subprocess.PIPE)
grep_cmd = subprocess.Popen(['grep', 'Date_and_Time__Original'], stdin=exif_cmd.stdout, stdout=subprocess.PIPE)
cut_cmd1 = subprocess.Popen(['cut', '-d', '>', '-f2'], stdin=grep_cmd.stdout, stdout=subprocess.PIPE)
cut_cmd2 = subprocess.Popen(['cut', '-d', '<', '-f1'], stdin=cut_cmd1.stdout, stdout=subprocess.PIPE)
cut_cmd1.stdout.close()
capture_time = cut_cmd2.communicate()[0].split("\n")[0]
# print captureTime
format_time = datetime.strptime(capture_time, "%Y:%m:%d %H:%M:%S")
return int(time.mktime(format_time.timetuple()))
def cam_make_query(image_name):
'''Function for returning the camera Make and Model'''
exif_cmd = subprocess.Popen(['exif', '-x', image_name], stdout=subprocess.PIPE)
grep_cmd = subprocess.Popen(['grep', 'Manufacturer'], stdin=exif_cmd.stdout, stdout=subprocess.PIPE)
cut_cmd1 = subprocess.Popen(['cut', '-d', '>', '-f2'], stdin=grep_cmd.stdout, stdout=subprocess.PIPE)
cut_cmd2 = subprocess.Popen(['cut', '-d', '<', '-f1'], stdin=cut_cmd1.stdout, stdout=subprocess.PIPE)
cut_cmd1.stdout.close()
cam_manufacturer = cut_cmd2.communicate()[0].split("\n")[0]
exif_cmd_model = subprocess.Popen(['exif', '-x', image_name], stdout=subprocess.PIPE)
grep_cmd_model = subprocess.Popen(['grep', 'Model'], stdin=exif_cmd_model.stdout, stdout=subprocess.PIPE)
cut_cmd1_model = subprocess.Popen(['cut', '-d', '>', '-f2'], stdin=grep_cmd_model.stdout, stdout=subprocess.PIPE)
cut_cmd2_model = subprocess.Popen(['cut', '-d', '<', '-f1'], stdin=cut_cmd1_model.stdout, stdout=subprocess.PIPE)
cut_cmd1_model.stdout.close()
cam_model = cut_cmd2_model.communicate()[0].split("\n")[0]
cam_dir = cam_manufacturer + "__" + cam_model
return cam_dir
def copy_all_images(src_dir):
'''Function for copying all the files from the source
directory and sub-directories to target directory.'''
for dirnames, filenames in os.walk(src_dir):
# Copy all files.
for filename in filenames:
image_name = os.path.join(dirnames, filename)
unix_time_stamp = timestamp_query(image_name)
camera = cam_make_query(image_name)
img_destination = TARGET_DIR + "/" + camera
## Create destination directory if not present
try:
os.makedirs(img_destination)
except OSError as exp:
if exp.errno != errno.EEXIST:
raise
## Start copying and renaming
final_image = img_destination + "/" + str(unix_time_stamp) + "_" + camera + ".JPG"
LOGGER.info("Copying and Renaming Image : %s to %s", image_name, final_image)
shutil.copy2(image_name, final_image)
INPUT_COMP_STRING = "\n*****\nPlease enter which directory images needs to be compressed and the \
quality level in the following format:\n\n\t\t\t\t*********************************\n\t\t\t\t \
<DirectoyName_#qQualityLevel>\n\t\t\t\t*********************************\n\
Eg.: Canon__Canon EOS 100D_#q90\n\nIn case you want to run compression on multiple directories \
please enter in csv format\nEg.: Canon__Canon EOS 100D_#q90, SAMSUNG__GT-I9100_q81\n\n*****\n\n\
Enter your input: "
def validate_user_compression_input():
'''Function for validating user input for compressing images inside specific directories.'''
while True:
try:
comp_input = input(INPUT_COMP_STRING)
if not comp_input:
raise ValueError("There wasn't any input!")
except ValueError as exp:
print(exp)
def main():
"""Start execution of the main program."""
if COMPRESSION == 'off':
copy_all_images(SOURCE_DIR)
else:
copy_all_images(SOURCE_DIR)
validate_user_compression_input()
# Executing the script.
if __name__ == "__main__":
main()