66from datetime import datetime
77import shutil
88
9+ def split_file_extension (file ):
10+ return os .path .splitext (file )
911
1012def message (file , converted ):
1113 current_time = datetime .now ().time ().strftime ("%H:%M:%S" )
@@ -20,7 +22,7 @@ def message(file, converted):
2022# convert RAW images function
2123def convert_raw (file , srcDir , tgtDir , extension = ".jpg" , resolution = ("100%" , "100%" )):
2224 try :
23- ext = "." + file . split ( "." )[ - 1 ]. lower ( )
25+ filename , _ = split_file_extension ( file )
2426 print (datetime .now ().time ().strftime ("%H:%M:%S" ) + " Converting: " + file )
2527 source = os .path .join (srcDir , file )
2628 with rawpy .imread (source ) as raw :
@@ -34,7 +36,7 @@ def convert_raw(file, srcDir, tgtDir, extension=".jpg", resolution=("100%", "100
3436 pil_image = pil_image .resize ((width , height ))
3537
3638 rgb = np .array (pil_image )
37- imageio .imsave (os .path .join (tgtDir , file . replace ( ext , "" ) + extension ), rgb )
39+ imageio .imsave (os .path .join (tgtDir , filename + extension ), rgb )
3840 message (file , True )
3941 except Exception as e :
4042 print (e )
@@ -59,15 +61,15 @@ def convert_file(file, srcDir, tgtDir, extension=".jpg", resolution=("100%", "10
5961 }
6062 save_format = mappings .get (extension , "JPEG" )
6163 try :
62- ext = "." + file . split ( "." )[ - 1 ]. lower ( )
64+ filename , _ = split_file_extension ( file )
6365 message (file , False )
6466 path = os .path .join (srcDir , file )
6567 im = Image .open (path )
6668 if resolution :
6769 width = calculate_image_dimension (im .width , resolution [0 ])
6870 height = calculate_image_dimension (im .height , resolution [1 ])
6971 im = im .resize ((width , height ))
70- im .save (os .path .join (tgtDir , file . replace ( ext , "" ) + extension ), save_format )
72+ im .save (os .path .join (tgtDir , filename + extension ), save_format )
7173 message (file , True )
7274 except :
7375 pass
@@ -86,27 +88,26 @@ def ai_2_pdf(file):
8688
8789# IT IS POINTLESS TO CONVERT WHAT IS ALREADY CONVERTED!!!!
8890def image_not_exists (image , tgtDir , tgtExt ):
89- ext = image . split ( "." )[ - 1 ]. lower ( )
90- target = os .path .join (tgtDir , image . replace ( ext , tgtExt . replace ( "." , "" )) )
91+ filename , _ = split_file_extension ( image )
92+ target = os .path .join (tgtDir , filename + tgtExt )
9193 if os .path .isfile (target ):
9294 return False
9395 else :
9496 return True
9597
9698
9799# here we check each file to decide what to do
98- def check_extension (file ):
100+ def check_file_type (file ):
99101 # get the extension as a String and check if the string is contained in the array extensionsForRawConversion
100- ext = "." + file . split ( "." )[ - 1 ]. lower ( )
102+ _ , ext = split_file_extension ( file )
101103 # set supported raw conversion extensions!
102- extensionsForRawConversion = [
104+ raw = [
103105 ".dng" ,
104106 ".raw" ,
105107 ".cr2" ,
106108 ".crw" ,
107109 ".erf" ,
108110 ".raf" ,
109- ".tif" ,
110111 ".kdc" ,
111112 ".dcr" ,
112113 ".mos" ,
@@ -124,11 +125,11 @@ def check_extension(file):
124125 ".mrw" ,
125126 ]
126127 # set supported imageio conversion extensions
127- extensionsForConversion = [".ppm" , ".psd" , ".tif " , ".webp" ]
128+ not_raw = [".ppm" , ".psd" , ".tiff " , ".webp" ]
128129
129- if ext in extensionsForRawConversion :
130+ if ext in raw :
130131 return "RAW"
131- if ext in extensionsForConversion :
132+ if ext in not_raw :
132133 return "NOT RAW"
133134 # check if an .ai exists and rename it to .pdf !
134135 ai_2_pdf (file )
0 commit comments