17
17
18
18
19
19
# create a message function
20
- def message (file , bool ):
20
+ def message (file , converted ):
21
21
screenLock .acquire ()
22
22
# if is converted
23
- if bool :
23
+ if converted :
24
24
print (datetime .now ().time ().strftime ('%H:%M:%S' ) + " Converted: " + file )
25
25
else :
26
26
print (datetime .now ().time ().strftime ('%H:%M:%S' ) + " Converting: " + file )
@@ -32,7 +32,6 @@ def message(file, bool):
32
32
os .makedirs (directory )
33
33
34
34
35
-
36
35
# convert RAW images function
37
36
def convert_raw (file , directory , tgtDir , extension = ".jpg" ):
38
37
# path = 'image.nef'
@@ -49,16 +48,17 @@ def convert_raw(file, directory, tgtDir, extension=".jpg"):
49
48
50
49
51
50
# convert function
52
- def convert_file (file , directory , tgtDir ):
51
+ def convert_file (file , directory , tgtDir , extension = ".jpg" ):
52
+ mappings = {
53
+ '.jpg' : 'JPEG' ,
54
+ '.png' : 'PNG' ,
55
+ }
56
+ save_format = mappings .get (extension , 'JPEG' )
53
57
try :
54
58
message (file , False )
55
59
path = os .path .join (tgtDir , file )
56
60
im = Image .open (path )
57
- # basewidth = 2048
58
- # wpercent = (basewidth/float(im.size[0]))
59
- # hsize = int((float(im.size[1])*float(wpercent)))
60
- # im = im.resize((basewidth,hsize), Image.ANTIALIAS)
61
- im .save (os .path .join (directory , file + ".jpg" ), "JPEG" , dpi = (600 , 600 ))
61
+ im .save (os .path .join (directory , file + extension ), save_format , dpi = (600 , 600 ))
62
62
message (file , True )
63
63
64
64
except :
@@ -85,13 +85,13 @@ def image_not_exists(e):
85
85
86
86
87
87
# here we check each file to decide what to do
88
- def checkExtension (ext ):
88
+ def check_extension (ext ):
89
89
# set supported raw conversion extensions!
90
90
extensionsForRawConversion = ['.dng' , '.raw' , '.cr2' , '.crw' , '.erf' , '.raf' , '.tif' , '.kdc' , '.dcr' , '.mos' ,
91
91
'.mef' , '.nef' , '.orf' , '.rw2' , '.pef' , '.x3f' , '.srw' , '.srf' , '.sr2' , '.arw' ,
92
92
'.mdc' , '.mrw' ]
93
93
# set supported imageio conversion extensions
94
- extensionsForConversion = ['.ppm' , '.psd' , '.tif' ]
94
+ extensionsForConversion = ['.ppm' , '.psd' , '.tif' , '.webp' ]
95
95
96
96
for i in extensionsForRawConversion :
97
97
if ext .lower ().endswith (i ):
@@ -107,20 +107,21 @@ def checkExtension(ext):
107
107
def main ():
108
108
print ('### PYTHON IMAGE CONVERTER ### \n \n ' )
109
109
110
- parser = optparse .OptionParser ("usage: " + sys .argv [0 ] + \
111
- "\n -s <source directory> \n ex: usage%prog -s C:\\ Users\\ USerName\\ Desktop\\ Photos_Dir \n After -s Specify the directory you will convert" )
112
- parser .add_option ('--s' , dest = 'nname' , type = 'string' , \
113
- help = 'specify your source directory!' )
114
- parser .add_option ('--ext' , dest = 'target_image_extension' , type = 'choice' , \
115
- default = ".jpg" , choices = ['.jpg' , '.png' ], help = 'the image format to be used for the converted images.' )
110
+ parser = optparse .OptionParser ("usage: " + sys .argv [0 ] +
111
+ "\n -s <source directory> \n ex: usage%prog --s "
112
+ "C:\\ Users\\ USerName\\ Desktop\\ Photos_Dir \n After --s Specify the directory you "
113
+ "will convert" )
114
+ parser .add_option ('--s' , dest = 'nname' , type = 'string' , help = 'specify your source directory!' )
115
+ parser .add_option ('--ext' , dest = 'target_image_extension' , type = 'choice' ,
116
+ default = ".jpg" , choices = ['.jpg' , '.png' ],
117
+ help = 'the image format to be used for the converted images.' )
116
118
(options , args ) = parser .parse_args ()
117
- if ( options .nname == None ) :
119
+ if options .nname is None :
118
120
print (parser .usage )
119
121
exit (0 )
120
122
else :
121
123
tgtDir = os .path .abspath (options .nname )
122
124
123
-
124
125
print ("Started conversion at : " + datetime .now ().time ().strftime ('%H:%M:%S' ) + '\n ' )
125
126
print ("Converting \n -> " + tgtDir + " Directory !\n " )
126
127
# find files to convert
@@ -129,21 +130,20 @@ def main():
129
130
for file in os .listdir (tgtDir ):
130
131
# CHECK IF WE HAVE CONVERTED THIS IMAGE! IF YES SKIP THE CONVERSIONS!
131
132
if image_not_exists (file ):
132
- if 'RAW' == checkExtension (file ):
133
- # Added multithreds to complete conversion faster
133
+ if 'RAW' == check_extension (file ):
134
+ # Added multithreading to complete conversion faster
134
135
t2 = Thread (target = convert_raw , args = (file , directory , tgtDir , options .target_image_extension ))
135
- t2 .start ();
136
-
137
- if 'NOT_RAW' == checkExtension (file ):
138
- t = Thread (target = convert_file , args = (file , directory , tgtDir ))
139
- t .start ();
140
- if file .endswith ('.tif' ):
141
- t = Thread (target = convert_file , args = (file , directory , tgtDir ))
142
- t .start ();
136
+ t2 .start ()
137
+
138
+ if 'NOT_RAW' == check_extension (file ):
139
+ t = Thread (target = convert_file , args = (file , directory , tgtDir , options .target_image_extension ))
140
+ t .start ()
141
+
143
142
print (" \n Converted Images are stored at - > \n " + os .path .abspath (directory ))
144
143
except :
145
144
print (
146
- "\n The directory at : \n " + tgtDir + " \n Are you sure is there? \n I am NOT! \n It NOT EXISTS !! Grrrr....\n \n " )
145
+ "\n The directory at : \n " + tgtDir + "\n Are you sure is there? \n I am NOT! \n It NOT EXISTS !! "
146
+ "Grrrr....\n \n " )
147
147
148
148
149
149
if __name__ == '__main__' :
0 commit comments