|
1 | 1 | from datetime import datetime |
2 | 2 | import os |
3 | | -from raw_image_converter.utils import check_extension, convert_file, convert_raw, image_not_exists |
| 3 | +from raw_image_converter.utils import ( |
| 4 | + check_extension, |
| 5 | + convert_file, |
| 6 | + convert_raw, |
| 7 | + image_not_exists, |
| 8 | + delete_directory, |
| 9 | +) |
4 | 10 | import argparse |
5 | 11 | import concurrent.futures |
6 | | -from colorama import * |
7 | | -#TODO use the extension argument of the command everywhere |
| 12 | +from colorama import Fore, Style |
8 | 13 |
|
9 | | -# All images are converted to jpg |
10 | 14 |
|
11 | | -# where to save our images |
12 | | -directory = "converted" |
13 | | - |
14 | | -# create a directory if needed to store our converted images! |
15 | | -if not os.path.exists(directory): |
16 | | - os.makedirs(directory) |
| 15 | +def tuple_type(values): |
| 16 | + values = values.split(",") |
| 17 | + return tuple(values) |
17 | 18 |
|
18 | 19 |
|
19 | 20 | def main(): |
20 | | - print('### PYTHON IMAGE CONVERTER ### \n \n') |
21 | | - |
22 | | - parser = argparse.ArgumentParser(description="Convert images to JPG") |
23 | | - parser.add_argument('-s', "--src", dest = "src_dir",help='specify the source directory!', required=True) # this argument is required to start the conversion |
24 | | - parser.add_argument("-t","--tgt", dest = "tgt_dir", help="specify the target directory!") # if there is no target directory given, the script will store the converted images in the source folder |
25 | | - parser.add_argument('-e',"--ext", dest = "ext", default=".jpg", choices=['.jpg', '.png'], |
26 | | - help='the image format to be used for the converted images.') |
27 | | - parser.add_argument("-f","--folder", dest = "seperate_folder", default=False, help="should the converted images be placed in a seperate folder") |
| 21 | + print("### PYTHON IMAGE CONVERTER ### \n \n") |
| 22 | + |
| 23 | + parser = argparse.ArgumentParser(description="Convert images to JPG/PNG") |
| 24 | + parser.add_argument( |
| 25 | + "-s", |
| 26 | + "--src", |
| 27 | + dest="src_dir", |
| 28 | + help="specify the source directory!", |
| 29 | + required=True, |
| 30 | + ) |
| 31 | + parser.add_argument( |
| 32 | + "-t", |
| 33 | + "--tgt", |
| 34 | + dest="tgt_dir", |
| 35 | + help="specify the target directory!", |
| 36 | + default="converted", |
| 37 | + ) |
| 38 | + parser.add_argument( |
| 39 | + "-e", |
| 40 | + "--ext", |
| 41 | + dest="ext", |
| 42 | + default=".jpg", |
| 43 | + choices=[".jpg", ".png"], |
| 44 | + help="the image format to be used for the converted images.", |
| 45 | + ) |
| 46 | + parser.add_argument( |
| 47 | + "-d", |
| 48 | + "--delete-source-directory", |
| 49 | + action="store_true", |
| 50 | + dest="delete_source_directory", |
| 51 | + default=False, |
| 52 | + help="Delete the source directory after the convesion", |
| 53 | + ) |
| 54 | + parser.add_argument( |
| 55 | + "-r", |
| 56 | + "--resolution", |
| 57 | + type=tuple_type, |
| 58 | + dest="resolution", |
| 59 | + default="100%,100%", |
| 60 | + help="image dimensions (width, height) as a tuple (using numbers 800,600 or using percentages 75%,75%)", |
| 61 | + ) |
| 62 | + |
28 | 63 | args = parser.parse_args() |
29 | | - |
30 | | - if args.tgt_dir == None: |
31 | | - if args.seperate_folder: |
32 | | - if not os.path.exists(args.src_dir + "/" + directory): |
33 | | - os.makedirs(args.src_dir + "/" + directory) |
34 | | - srcDir = args.src_dir |
35 | | - tgtDir = args.src_dir + directory |
36 | | - else: |
37 | | - srcDir = args.src_dir |
38 | | - tgtDir = args.src_dir |
39 | | - else: |
40 | | - if args.seperate_folder: # if the converted files should be stored in a seperate folder, create the folder and add the images to the folder |
41 | | - if not os.path.exists(args.tgt_dir + "/" + directory): |
42 | | - os.makedirs(args.tgt_dir + "/" + directory) |
43 | | - srcDir = os.path.abspath(args.src_dir) |
44 | | - tgtDir = os.path.abspath(args.tgt_dir + directory) |
45 | | - else: # if the converted files sould be kept in the source folder, |
46 | | - srcDir = os.path.abspath(args.src_dir) |
47 | | - tgtDir = os.path.abspath(args.tgt_dir) |
| 64 | + |
| 65 | + srcDir = os.path.abspath(args.src_dir) |
| 66 | + tgtDir = os.path.abspath(args.tgt_dir) |
| 67 | + resolution = args.resolution |
| 68 | + |
| 69 | + # Handle one dimensional tuples |
| 70 | + if len(resolution) == 1: |
| 71 | + resolution = (resolution[0], resolution[0]) |
| 72 | + |
| 73 | + if not os.path.exists(tgtDir): |
| 74 | + os.makedirs(tgtDir) |
48 | 75 |
|
49 | 76 | # find files to convert |
50 | 77 | try: |
51 | | - with concurrent.futures.ProcessPoolExecutor() as executor: |
52 | | - print("Started conversion at : " + datetime.now().time().strftime('%H:%M:%S') + '\n') |
53 | | - print("Converting -> " + srcDir + " Directory !\n") |
| 78 | + with concurrent.futures.ProcessPoolExecutor() as executor: |
| 79 | + print( |
| 80 | + "Started conversion at : " |
| 81 | + + datetime.now().time().strftime("%H:%M:%S") |
| 82 | + + "\n" |
| 83 | + ) |
| 84 | + print("Converting -> " + srcDir + " Directory !\n") |
54 | 85 | for file in os.listdir(srcDir): |
55 | | - #TODO also use the extension from the command as a parameter to the image_not_exists function |
56 | 86 | if image_not_exists(file, tgtDir, args.ext): |
57 | | - if 'RAW' == check_extension(file): |
58 | | - executor.submit( |
59 | | - convert_raw, |
60 | | - file, |
61 | | - srcDir, |
62 | | - tgtDir, |
63 | | - args.ext, |
64 | | - ) |
65 | | - |
66 | | - if 'NOT_RAW' == check_extension(file): |
| 87 | + if "RAW" == check_extension(file): |
| 88 | + executor.submit( |
| 89 | + convert_raw, |
| 90 | + file, |
| 91 | + srcDir, |
| 92 | + tgtDir, |
| 93 | + args.ext, |
| 94 | + resolution, |
| 95 | + ) |
| 96 | + |
| 97 | + if "NOT_RAW" == check_extension(file): |
67 | 98 | executor.submit( |
68 | 99 | convert_file, |
69 | 100 | file, |
70 | 101 | srcDir, |
71 | 102 | tgtDir, |
72 | 103 | args.ext, |
| 104 | + resolution, |
73 | 105 | ) |
74 | 106 | else: |
75 | | - print(f"{Fore.GREEN}File " + file + f" is already converted!{Style.RESET_ALL}"+" \n ") |
| 107 | + print( |
| 108 | + f"{Fore.GREEN}File " |
| 109 | + + file |
| 110 | + + f" is already converted!{Style.RESET_ALL}" |
| 111 | + + " \n " |
| 112 | + ) |
| 113 | + |
| 114 | + print( |
| 115 | + f"{Fore.GREEN}Converted Images are stored at - > " |
| 116 | + + os.path.abspath(tgtDir) |
| 117 | + + f"{Style.RESET_ALL}" |
| 118 | + ) |
76 | 119 |
|
77 | | - print(f"{Fore.GREEN}Converted Images are stored at - > " + os.path.abspath(tgtDir)+f"{Style.RESET_ALL}") |
| 120 | + if args.delete_source_directory: |
| 121 | + delete_directory(srcDir) |
78 | 122 |
|
79 | 123 | except Exception as e: |
80 | 124 | print(f"{Fore.RED}ERROR IN APPLICATION{Style.RESET_ALL}" + e) |
81 | 125 |
|
| 126 | + |
82 | 127 | if __name__ == "__main__": |
83 | 128 | main() |
0 commit comments