|
| 1 | +import sys |
1 | 2 | from PIL import Image
|
2 | 3 | import os
|
3 | 4 | from threading import *
|
|
13 | 14 |
|
14 | 15 | # where to save our images
|
15 | 16 | directory = "converted"
|
| 17 | + |
| 18 | + |
16 | 19 | # create a message function
|
17 |
| -def message(file,bool): |
18 |
| - screenLock.acquire() |
19 |
| - # if is converted |
20 |
| - if bool: |
21 |
| - print datetime.now().time().strftime('%H:%M:%S') + " Converted: "+ file |
22 |
| - else: |
23 |
| - print datetime.now().time().strftime('%H:%M:%S') + " Converting: "+ file |
24 |
| - screenLock.release() |
| 20 | +def message(file, bool): |
| 21 | + screenLock.acquire() |
| 22 | + # if is converted |
| 23 | + if bool: |
| 24 | + print(datetime.now().time().strftime('%H:%M:%S') + " Converted: " + file) |
| 25 | + else: |
| 26 | + print(datetime.now().time().strftime('%H:%M:%S') + " Converting: " + file) |
| 27 | + screenLock.release() |
| 28 | + |
25 | 29 |
|
26 | 30 | # create a directory if needed to store our converted images!
|
27 | 31 | if not os.path.exists(directory):
|
28 | 32 | os.makedirs(directory)
|
29 | 33 |
|
| 34 | + |
| 35 | + |
30 | 36 | # convert RAW images function
|
31 |
| -def convert_raw(file,directory,tgtDir): |
32 |
| - #path = 'image.nef' |
33 |
| - |
34 |
| - try: |
35 |
| - message(file,False) |
36 |
| - path = tgtDir + '\\'+file |
37 |
| - with rawpy.imread(path) as raw: |
38 |
| - rgb = raw.postprocess() |
39 |
| - imageio.imsave( directory +'\\' +file+'.jpg', rgb) |
40 |
| - message(file,True) |
41 |
| - except: |
42 |
| - pass |
| 37 | +def convert_raw(file, directory, tgtDir): |
| 38 | + # path = 'image.nef' |
| 39 | + |
| 40 | + try: |
| 41 | + message(file, False) |
| 42 | + path = os.path.join(tgtDir, file) |
| 43 | + with rawpy.imread(path) as raw: |
| 44 | + rgb = raw.postprocess() |
| 45 | + imageio.imsave(os.path.join(directory, file + '.jpg'), rgb) |
| 46 | + message(file, True) |
| 47 | + except: |
| 48 | + pass |
| 49 | + |
| 50 | + try: |
| 51 | + message(file, False) |
| 52 | + path = os.path.join(tgtDir, file) |
| 53 | + with rawpy.imread(path) as raw: |
| 54 | + rgb = raw.postprocess() |
| 55 | + imageio.imsave(os.path.join(directory, file + '.png'), rgb) |
| 56 | + message(file, True) |
| 57 | + except: |
| 58 | + pass |
| 59 | + |
43 | 60 |
|
44 | 61 | # convert function
|
45 |
| -def convert_file(file,directory,tgtDir): |
46 |
| - try: |
47 |
| - message(file,False) |
48 |
| - path = tgtDir + '\\'+file |
49 |
| - im = Image.open(path) |
50 |
| - #basewidth = 2048 |
51 |
| - #wpercent = (basewidth/float(im.size[0])) |
52 |
| - #hsize = int((float(im.size[1])*float(wpercent))) |
53 |
| - #im = im.resize((basewidth,hsize), Image.ANTIALIAS) |
54 |
| - im.save(directory +'/' + file + ".jpg", "JPEG",dpi=(600,600)) |
55 |
| - message(file,True) |
56 |
| - |
57 |
| - except: |
58 |
| - pass |
| 62 | +def convert_file(file, directory, tgtDir): |
| 63 | + try: |
| 64 | + message(file, False) |
| 65 | + path = os.path.join(tgtDir, file) |
| 66 | + im = Image.open(path) |
| 67 | + # basewidth = 2048 |
| 68 | + # wpercent = (basewidth/float(im.size[0])) |
| 69 | + # hsize = int((float(im.size[1])*float(wpercent))) |
| 70 | + # im = im.resize((basewidth,hsize), Image.ANTIALIAS) |
| 71 | + im.save(os.path.join(directory, file + ".jpg"), "JPEG", dpi=(600, 600)) |
| 72 | + message(file, True) |
| 73 | + |
| 74 | + except: |
| 75 | + pass |
59 | 76 |
|
60 | 77 |
|
61 | 78 | # rename .ai 2 pdf and problem solved!
|
62 | 79 | def ai_2_pdf(e):
|
63 |
| - if e.endswith('.ai'): |
64 |
| - os.rename(e, directory +'\\' +e+'.pdf') |
65 |
| - print datetime.now().time().strftime('%H:%M:%S')+ " Converted ai 2 pdf : " + directory +'\\' +e+'.pdf' |
| 80 | + if e.endswith('.ai'): |
| 81 | + os.rename(e, os.path.join(directory, e + '.pdf')) |
| 82 | + print( |
| 83 | + datetime.now().time().strftime('%H:%M:%S') + " Converted ai 2 pdf : " + os.path.join(directory, e + '.pdf')) |
66 | 84 |
|
67 | 85 |
|
68 | 86 | # IT IS POINTLESS TO CONVERT WHAT IS ALREADY CONVERTED!!!!
|
69 | 87 | def image_not_exists(e):
|
70 |
| - if os.path.isfile(directory +'\\' +e+'.jpg'): |
71 |
| - screenLock.acquire() |
72 |
| - print "File " + e + " is already converted! \n" |
73 |
| - screenLock.release() |
74 |
| - return False |
75 |
| - else: |
76 |
| - return True |
| 88 | + if os.path.isfile(os.path.join(directory, e + '.jpg')): |
| 89 | + screenLock.acquire() |
| 90 | + print("File " + e + " is already converted! \n") |
| 91 | + screenLock.release() |
| 92 | + return False |
| 93 | + else: |
| 94 | + return True |
| 95 | + |
| 96 | + |
77 | 97 | # here we check each file to decide what to do
|
78 | 98 | def checkExtension(ext):
|
79 |
| - # set supported raw conversion extensions! |
80 |
| - extensionsForRawConversion = ['.dng','.raw','.cr2','.crw','.erf','.raf','.tif','.kdc','.dcr','.mos', |
81 |
| - '.mef','.nef','.orf','.rw2','.pef','.x3f','.srw','.srf','.sr2','.arw','.mdc','.mrw'] |
82 |
| - # set supported imageio conversion extensions |
83 |
| - extensionsForConversion = ['.ppm','.psd','.tif'] |
84 |
| - |
85 |
| - for i in extensionsForRawConversion: |
86 |
| - if ext.lower().endswith(i): |
87 |
| - return 'RAW' |
88 |
| - |
89 |
| - for e in extensionsForConversion: |
90 |
| - if ext.lower().endswith(e): |
91 |
| - return 'NOT_RAW' |
92 |
| - # check if an .ai exists and rename it to .pdf ! |
93 |
| - ai_2_pdf(ext) |
| 99 | + # set supported raw conversion extensions! |
| 100 | + extensionsForRawConversion = ['.dng', '.raw', '.cr2', '.crw', '.erf', '.raf', '.tif', '.kdc', '.dcr', '.mos', |
| 101 | + '.mef', '.nef', '.orf', '.rw2', '.pef', '.x3f', '.srw', '.srf', '.sr2', '.arw', |
| 102 | + '.mdc', '.mrw'] |
| 103 | + # set supported imageio conversion extensions |
| 104 | + extensionsForConversion = ['.ppm', '.psd', '.tif'] |
| 105 | + |
| 106 | + for i in extensionsForRawConversion: |
| 107 | + if ext.lower().endswith(i): |
| 108 | + return 'RAW' |
94 | 109 |
|
| 110 | + for e in extensionsForConversion: |
| 111 | + if ext.lower().endswith(e): |
| 112 | + return 'NOT_RAW' |
| 113 | + # check if an .ai exists and rename it to .pdf ! |
| 114 | + ai_2_pdf(ext) |
95 | 115 |
|
96 | 116 |
|
97 | 117 | def main():
|
98 |
| - print '### PYTHON IMAGE CONVERTER ### \n \n' |
99 |
| - |
100 |
| - parser = optparse.OptionParser("usage%prog "+ \ |
101 |
| - "-s <source directory> \n ex: usage%prog -s C:\\Users\\USerName\\Desktop\\Photos_Dir \n After -s Specify the directory you will convert") |
102 |
| - parser.add_option('-s', dest='nname', type='string',\ |
103 |
| - help='specify your source directory!') |
104 |
| - (options, args) = parser.parse_args() |
105 |
| - if (options.nname == None): |
106 |
| - print parser.usage |
107 |
| - exit(0) |
108 |
| - else: |
109 |
| - tgtDir = os.path.abspath(options.nname) |
110 |
| - |
111 |
| - |
112 |
| - |
113 |
| - |
114 |
| - |
115 |
| - |
116 |
| - |
117 |
| - print "Started conversion at : " + datetime.now().time().strftime('%H:%M:%S')+ '\n' |
118 |
| - print "Converting \n -> " + tgtDir + " Directory !\n" |
119 |
| - # find files to convert |
120 |
| - try: |
121 |
| - |
122 |
| - for file in os.listdir(tgtDir): |
123 |
| - # CHECK IF WE HAVE CONVERTED THIS IMAGE! IF YES SKIP THE CONVERSIONS! |
124 |
| - if image_not_exists(file): |
125 |
| - if 'RAW' == checkExtension(file): |
126 |
| - |
127 |
| - # Added multithreds to complete conversion faster |
128 |
| - t2 = Thread(target = convert_raw, args = (file,directory,tgtDir)) |
129 |
| - t2.start(); |
130 |
| - |
131 |
| - if 'NOT_RAW' == checkExtension(file): |
132 |
| - |
133 |
| - t = Thread(target = convert_file, args = (file,directory,tgtDir)) |
134 |
| - t.start(); |
135 |
| - if file.endswith('.tif'): |
136 |
| - t = Thread(target = convert_file, args = (file,directory,tgtDir)) |
137 |
| - t.start(); |
138 |
| - print " \n Converted Images are stored at - > \n " + os.path.abspath(directory) |
139 |
| - except: |
140 |
| - print "\n The directory at : \n " + tgtDir + " \n Are you sure is there? \n I am NOT! \n It NOT EXISTS !! Grrrr....\n\n" |
141 |
| - |
142 |
| - |
143 |
| - |
144 |
| - |
| 118 | + print('### PYTHON IMAGE CONVERTER ### \n \n') |
| 119 | + |
| 120 | + parser = optparse.OptionParser("usage: " + sys.argv[0] + \ |
| 121 | + "\n-s <source directory> \n ex: usage%prog -s C:\\Users\\USerName\\Desktop\\Photos_Dir \n After -s Specify the directory you will convert") |
| 122 | + parser.add_option('-s', dest='nname', type='string', \ |
| 123 | + help='specify your source directory!') |
| 124 | + (options, args) = parser.parse_args() |
| 125 | + if (options.nname == None): |
| 126 | + print(parser.usage) |
| 127 | + exit(0) |
| 128 | + else: |
| 129 | + tgtDir = os.path.abspath(options.nname) |
| 130 | + |
| 131 | + print("Started conversion at : " + datetime.now().time().strftime('%H:%M:%S') + '\n') |
| 132 | + print("Converting \n -> " + tgtDir + " Directory !\n") |
| 133 | + # find files to convert |
| 134 | + try: |
| 135 | + |
| 136 | + for file in os.listdir(tgtDir): |
| 137 | + # CHECK IF WE HAVE CONVERTED THIS IMAGE! IF YES SKIP THE CONVERSIONS! |
| 138 | + if image_not_exists(file): |
| 139 | + if 'RAW' == checkExtension(file): |
| 140 | + # Added multithreds to complete conversion faster |
| 141 | + t2 = Thread(target=convert_raw, args=(file, directory, tgtDir)) |
| 142 | + t2.start(); |
| 143 | + |
| 144 | + if 'NOT_RAW' == checkExtension(file): |
| 145 | + t = Thread(target=convert_file, args=(file, directory, tgtDir)) |
| 146 | + t.start(); |
| 147 | + if file.endswith('.tif'): |
| 148 | + t = Thread(target=convert_file, args=(file, directory, tgtDir)) |
| 149 | + t.start(); |
| 150 | + print(" \n Converted Images are stored at - > \n " + os.path.abspath(directory)) |
| 151 | + except: |
| 152 | + print( |
| 153 | + "\n The directory at : \n " + tgtDir + " \n Are you sure is there? \n I am NOT! \n It NOT EXISTS !! Grrrr....\n\n") |
| 154 | + |
| 155 | + |
145 | 156 | if __name__ == '__main__':
|
146 |
| - main() |
| 157 | + main() |
0 commit comments