-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold.type2.py
170 lines (137 loc) · 4.13 KB
/
old.type2.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
# -*- coding:utf-8 -*-
import os
import sys
from PIL import Image
import threading
import time
import logging
# Dir
if len(sys.argv) > 1:
fileDir = sys.argv[1]
else:
fileDir = 'top'
# config
_NEW_PREFIX = "new"
_ENABLE_LOGGING = True
_IGNORE = ['.db', '.txt', '.rar', '.zip']
def configLogging():
if not _ENABLE_LOGGING:
return
_messagefmt = '[%(asctime)s] %(message)s'
logging.basicConfig(level=logging.INFO,
format=_messagefmt,
filename='transform.log',
filemode='a')
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter(fmt=_messagefmt)
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
def output(str):
if _ENABLE_LOGGING:
logging.info(str)
else:
print(str)
class Task:
def __init__(self, oldpath, newpath, press = 50):
self.oldpath = oldpath
self.newpath = newpath
if press <= 0 or press > 100:
press = 50
self.press = press
def GetTask():
_threadLock.acquire()
task = ""
if len(_taskList) > 0:
task = _taskList[0]
_taskList.remove(task)
_threadLock.release()
return task
def AddTask(oldpath, newpath, press = 50):
global _allTask
task = Task(oldpath, newpath, press)
_taskList.append(task)
_allTask += 1
_threadLock = threading.Lock()
_taskList = []
_allTask = 0
_done = 0
def generateTask():
_walk = os.walk(fileDir)
for root,dirs,files in _walk:
new_root = root.replace(fileDir , _NEW_PREFIX + fileDir)
for d in dirs:
if not os.path.exists(new_root):
os.mkdir(new_root)
path = os.path.join(new_root,d)
#output(path)
if not os.path.exists(path):
os.mkdir(path)
for f in files:
# 跳过指定后缀的文件
ext = os.path.splitext(f)[-1]
if ext in _IGNORE:
continue
oldpath = os.path.join(root,f)
newpath = os.path.join(new_root,f)
if os.path.exists(newpath):
try :
#output('exists : %s' % newpath)
pass
except UnicodeEncodeError as e:
output(e)
continue
AddTask(oldpath, newpath, 50)
def thread_do_task(id):
global _done,_allTask
while _done < _allTask:
task = GetTask()
if task == "":
time.sleep(0.5)
continue
try:
sImage = Image.open(task.oldpath)
except IOError as e:
output(e)
_threadLock.acquire()
_done += 1
_threadLock.release()
continue # 跳过非图像类型的文件
w,h = sImage.size
dImg = sImage.resize((int(w/2),int(h/2)), Image.ANTIALIAS)
dImg.save(task.newpath)
try :
output('convert : %s (%d/%d) threading %d' % (task.newpath, _done,_allTask, id))
except UnicodeEncodeError as e:
output(e)
_threadLock.acquire()
_done += 1
_threadLock.release()
sImage.close()
class myThread (threading.Thread): #继承父类threading.Thread
def __init__(self, id):
threading.Thread.__init__(self)
self.id = id
def run(self): #把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
thread_do_task(self.id)
def doAllTask():
threads = []
for i in range(15):
thread = myThread(i)
thread.start()
threads.append(thread)
for t in threads:
t.join()
if __name__ == "__main__":
configLogging()
_time_start = time.clock()
_time_end = 0
generateTask()
doAllTask()
_time_end = time.clock()
_time_used = _time_end - _time_start
_average = 0 if _allTask == 0 else (_time_used/_allTask)
output("Multithreading Mode:")
output("All done! used time %.2fs" % _time_used)
output("%.2fs per image" % _average)
output("All Task : %d" % _allTask)